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

Comments

  1. 08 Jan 2009 at 17:37
    My.Computer.Network.IsAvailable =)
  2. 02 Mar 2008 at 23:06

    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 


  3. 31 Aug 2006 at 12:16

    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

     

     

  4. 21 Nov 2005 at 15:03

    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!!!

  5. 25 Aug 2005 at 19:13

    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

  6. 28 Sep 2004 at 07:25

    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


  7. 11 Apr 2004 at 21:23

    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

  8. 23 Oct 2003 at 08:41

    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

  9. 01 Jan 1999 at 00:00

    This thread is for discussions of Is an Internet Connection Available?.

Leave a comment

Sign in or Join us (it's free).

Karl Moore
AddThis

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...

We'd love to hear what you think! Submit ideas or give us feedback