Library code snippets
Is an Internet Connection Available?
Checking whether an Internet connection is available isn’t always as
easy as it sounds.
Admittedly, there is a Windows API call that can check whether a connection
exists, but it’s extremely fragile and returns incorrect results if the
machine has never had Internet Explorer configured correctly. Oops.
The best method is to actually make a Web request and see whether it works. If it does, you’ve got your connection. The following neat code snippet does exactly that. Just call IsConnectionAvailable and check the return value:
Public Function IsConnectionAvailable() As Boolean
' Returns True if connection is available
' Replace www.yoursite.com with a
site that
' is guaranteed to be online - perhaps your
' corporate site, or microsoft.com
Dim objUrl As New System.Uri("http://www.yoursite.com/")
' Setup WebRequest
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objResp As System.Net.WebResponse
Try
' Attempt to get response and return True
objResp = objWebReq.GetResponse
objResp.Close()
objWebReq = Nothing
Return True
Catch ex As Exception
' Error, exit and return False
objResp.Close()
objWebReq = Nothing
Return False
End Try
Here’s how you might use this function in your application:
If IsConnectionAvailable() = True Then
MessageBox.Show("You are online!")
End If
Related articles
Related discussion
-
How to write a query set to excel using vb.net
by BarbaMariolino (1 replies)
-
Very Urgent regarding deleting the images from a folder
by rameshbandi (2 replies)
-
Block Accessing MSSQL 2000
by militia (0 replies)
-
.NET Developer in Ghana Required....
by sysview (0 replies)
-
Sending SMS to mobile using secure gateway from VB.net 2008 c#
by pratikasthana17 (0 replies)
Related podcasts
-
xpert to Expert: Inside Concurrent Basic (CB)
"Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...
LooooooooooL
I first disconnect my ADSL connection, then started this code.
It tooks a second for my modem to reconnect again and this function returns "TRUE"
I will need different solution
Real simple and neat chunk of code that could be used in any kind of Application, whether it be a Console or a windows Application and not just a Web Application.
I had used the same code to make a web request to an ASP page from my Windows Application.
Regards
you can not use VB.NET in ASP pages... Active Server Pages does not support .NET.
You have to use Visual Studio and create a .NET web page, and the file extension will be
.aspx
Happy programming!!!
Helo, I would like to ask if you know how to make internet connection and terminate it in VB.NET? if you know, please send me the code. thanks a lot
The exception on this errors out if you are offline. To fix this comment out the objresp.close line under the catch exception.
You can't close something that didn't open
I get this error when I try to run it on my IIS, please help me!
Error Type:
Microsoft VBScript compilation (0x800A0400)
Expected statement
/test.asp, line 2, column 40
Public Function IsConnectionAvailable() As Boolean
---------------------------------------^
I saved the page in a page name test.asp
and the code to check at the botton of the same page.
Cipto
For some reason or another I had to take out a line, the functino I used is as follows:
Public Function IsConnectionAvailable() As Boolean
' Returns True if connection is available
' Replace www.yoursite.com with a site that
' is guaranteed to be online - perhaps your
' corporate site, or microsoft.com
Dim objUrl As New System.Uri("http://www.yoursite.com/")
' Setup WebRequest
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objResp As System.Net.WebResponse
Try
' Attempt to get response and return True
objResp = objWebReq.GetResponse
objResp.Close()
objWebReq = Nothing
Return True
Catch ex As Exception
' Error, exit and return False
objWebReq = Nothing
Return False
End Try
End Function
The only change I made was taking out the objResp.Close() after the catch. It appears that is not created on a failure so the close function errors.
Nathan
This thread is for discussions of Is an Internet Connection Available?.