Check internet connection???

mythinky

Member
Joined
Jun 4, 2004
Messages
20
Programming Experience
1-3
How to check whether the internet connection is connected or not? I want to send email from my application, but it will raise problem if it's not connected.
 
Try this.

Is use this code, it sends a web request to a site, if it is successfull, it returns 1, otherwise it returns 0.

VB.NET:
  [size=2]
[/size][size=2][color=#008000]'Check Web Connection

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] WebRequest [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Net.WebRequest

[/size][size=2][color=#008000]' Initialize the WebRequest.

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myRequest [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Net.WebRequest = WebRequest.Create("http://www.microsoft.com")

[/size][size=2][color=#008000]' Return the response. 

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myResponse [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Net.WebResponse = myRequest.GetResponse()

[/size][size=2][color=#008000]' Code to use the WebResponse goes here.

[/color][/size][size=2]internetcon.Text = "1"

[/size][size=2][color=#008000]' Close the response to free resources.

[/color][/size][size=2]myResponse.Close()
 
[color=#008000]' Use the connection[/color]
[size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] internetcon.Text = "1" [/size][size=2][color=#0000ff]Then

[/color][/size][size=2][/size][size=2][color=#008000]   'Enter your code here.

[/color][/size][size=2][/size][size=2][color=#0000ff]Else[/color][/size][size=2] : MsgBox("The internet is not connected. Please connect")

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If[/color][/size]

[/size]

You will also need a Reference to System.Web.
I use a textbox called "internetcon", so if it's connected it returns 1, and then continues to send the email. Other wise use some error code to state that the internet is not connected.

I got no idea if thats the best way, hope it helps.
 
Back
Top