Best Way to Check Internet Connection (Asked Again)

  • Thread starter Thread starter tropicwhisper
  • Start date Start date
T

tropicwhisper

Guest
All,

I need to check for an active Internet connection. Here's the code I'm using, but it returns TRUE over 75% of the time when I have it check for a bogus website like "http://dsjjjsbdcjhss.com"

NOTE: I can't check Internet connectivity via Ping because my client's network (WILL NOT) allow pings... so that 's out.

NOTE: There have been suggestions to just (catch the exception) and handle that. Isn't there a more elegant method? Also, it doesn't throw an exception when I test for a bogus website 75% of the time.

NOTE: I can't use IsAvailable because my application is a ClickOnce (Microsoft message below).

Code that I'm using:

Dim iTimeout_HTTP As Int16 = 5000
Dim success As Boolean = False

' The Website MUST be in this format: Google

'''''sWebsite = "Google"

' Test Point: Start
sWebsite = "http://dsjjjsbdcjhss.com"
' Test Point: End

Dim web_request As System.Net.HttpWebRequest = Nothing
Dim web_response As System.Net.HttpWebResponse = Nothing

web_request = CType(System.Net.HttpWebRequest.Create(sWebsite), System.Net.HttpWebRequest)
web_request.Timeout = iTimeout_HTTP
web_response = CType(web_request.GetResponse(), System.Net.HttpWebResponse)
web_request.Abort()

If web_response.StatusCode = System.Net.HttpStatusCode.OK Then
success = True
Else
success = False
End If

web_request = Nothing
web_response = Nothing

Return success


I can't use IsAvailable as in ...

If My.Computer.Network.IsAvailable = True Then
MsgBox("Computer is connected.")
Else
MsgBox("Computer is not connected.")
End If

... because Microsoft states here that:

IsAvailable always returns False when called from a Click Once application or if the user does not have NetworkInformationPermission.

My application is a ClickOnce application.

Network.IsAvailable Property (Microsoft.VisualBasic.Devices)


What can I do to check Internet Conncetion?

ADawn





ADawn

Continue reading...
 
Back
Top