Comprobar conexión a internet desde VB
Comprobar conexión a internet desde VS
Para conocer si hay conexión a Internet en caso tengamos un WebBrowser en la aplicación, para VB.NET se puede hacer esto:
Imports
System.Net
Public
Class Form1
Private Sub Form1_Load(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
MyBase.Load
compruebaConexion()
End Sub
Public Function compruebaConexion() As
Boolean
Dim
request As WebRequest
Dim
response As WebResponse
Dim Url
As New Uri("http://www.pabletoreto.blogspot.com")
Try
'Creamos la
request
request = System.Net.WebRequest.Create(Url)
'POnemos un tiempo limite
request.Timeout = 5000
'ejecutamos la request
response = request.GetResponse
response.Close()
request = Nothing
Label2.Text = System.Environment.MachineName & "si hay conexion a internet"
Return
True
Catch
ex As Exception
'si ocurre un error, devolvemos error
request
= Nothing
Label2.Text = "No hay conexion a internet"
Return
False
End Try
End Function
End
Class
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Net;
namespace
WindowsFormsApplication1
{
public partial class Form2 : Form
{
public
Form2()
{
InitializeComponent();
}
private
void Form2_Load(object
sender, EventArgs e)
{
compruebaConexion();
}
public bool compruebaConexion()
{
WebRequest
request = null;
WebResponse
response = null;
Uri
Url = new Uri("http://www.pabletoreto.blogspot.com");
try
{
//Creamos
la request
request = System.Net.WebRequest.Create(Url);
//POnemos un tiempo limite
request.Timeout = 5000;
//ejecutamos la request
response = request.GetResponse();
response.Close();
request = null;
label1.Text = System.Environment.MachineName + "si
hay conexion a internet";
return
true;
}
catch (Exception
ex)
{
//si ocurre un error, devolvemos error
request = null;
label1.Text = "No hay conexion a internet";
return
false;
}
}
}
}
COMENTA LA PUBLICACION