Holas,
Me baje este codigo de internet que funciona muy bien
Public Shared Function CheckForInternetConnection() As Boolean
Try
Using client = New WebClient()
Using stream = client.OpenRead("http://www.google.com")
Return True
End Using
End Using
Catch
Return False
End Try
End Function
Mi preocupación es que me ha dado unos falsos positivos en mi Red tengo un portal cautivo me preocupa que al verificar la URL este le devuelva el portal y lo tome como un si en esta función.
Una solución podría ser verificar las cabeceras ¿?
Saludos
lee data de la pagina o lee cabeceras, tienen que coincidir
Hola,
Menudo susto me haz dado jajaja cuando pense en esa solución y modifique el mensaje fue muy tarde ya me habias respondido....
Jajajaja
Vale buscare algo para leer las cabeceras..
Saludos
Deberías comprobar la disponibilidad de tu adaptador, no la disponibilidad de un host (ya sea Google o cual sea)...
Esto lo he sacado de mi framework comercial ElektroKit:
Imports System.Net.NetworkInformation
Namespace ElektroKit.Core.NET.Tools
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Contains networking related utilities.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Public NotInheritable Class NetworkUtil
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Prevents a default instance of the <see cref="NetworkUtil"/> class from being created.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
<DebuggerNonUserCode>
Private Sub New()
End Sub
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets a value indicating whether at least one of the current network adapters is capable of connecting to Internet.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <value>
''' <see langword="True"/> if at least one of the current network adapters is capable of connecting to Internet;
''' otherwise, <see langword="False"/>.
''' </value>
''' ----------------------------------------------------------------------------------------------------
Public Shared ReadOnly Property IsNetworkAvailable As Boolean
<DebuggerStepThrough>
Get
Return NetworkUtil.GetNetworkAvailable()
End Get
End Property
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Evaluate the online network adapters to determine if at least one of them is capable of connecting to Internet.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' <see langword="True"/> if at least one of the current network adapters is capable of connecting to Internet;
''' otherwise, <see langword="False"/>.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Private Shared Function GetNetworkAvailable() As Boolean
' Only recognizes changes related to Internet adapters.
If NetworkInterface.GetIsNetworkAvailable() Then
' However, this will include all adapters.
Dim interfaces As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For Each ni As NetworkInterface In interfaces
' Filter so we see only Internet adapters.
If ni.OperationalStatus = OperationalStatus.Up Then
If (ni.NetworkInterfaceType <> NetworkInterfaceType.Tunnel) AndAlso
(ni.NetworkInterfaceType <> NetworkInterfaceType.Loopback) Then
Dim statistics As IPv4InterfaceStatistics = ni.GetIPv4Statistics()
' All testing seems to prove that once an interface comes online
' it has already accrued statistics for both received and sent...
If (statistics.BytesReceived > 0) AndAlso (statistics.BytesSent > 0) Then
Return True
End If
End If
End If
Next ni
End If
Return False
End Function
End Class
End Namespace
Saludos.
Hola Elektro,
Este codigo es para comprobar si hay algun adaptador disponible para conectarse a internet, o me equivoco.
Me quedo con el codigo porque se que me sera útil pero a lo que me referia es al falso positivo que me genera esa función a causa del portal cautivo que tengo en mi Red.
Por eso era que decia de revisar las cabeceras de la URL asi sabria si es el portal o la pagina de google.
Saludos