Posted By

blackf0rk on 05/12/09


Tagged

Net connection internet 35


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

mjnbrn
tiffle
D4RKCODE


Test for Internet Connection


 / Published in: VB.NET
 

URL: http://forums.devbuzz.com/tm.asp?m=15988&p=1&tmode=1

This is a simple function to test for an internet connection. Originally grabbed from "devbuzz" at the devbuzz forums (see link) and now modified for VB.NET 3.5

  1. Public Function GotInternet() As Boolean
  2. Dim req As System.Net.HttpWebRequest
  3. Dim res As System.Net.HttpWebResponse
  4. GotInternet = False
  5. Try
  6. req = CType(System.Net.HttpWebRequest.Create("http://www.google.com"), System.Net.HttpWebRequest)
  7. res = CType(req.GetResponse(), System.Net.HttpWebResponse)
  8. req.Abort()
  9. If res.StatusCode = System.Net.HttpStatusCode.OK Then
  10. GotInternet = True
  11. End If
  12. Catch weberrt As System.Net.WebException
  13. GotInternet = False
  14. Catch except As Exception
  15. GotInternet = False
  16. End Try
  17. End Function

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: breakskater on August 28, 2010

HttpWebRequest takes to long to test the connection. If you are already accessing a web service, then it is easier and quicker to attempt to connect to the web service for an internet connectivity check..

Posted By: saviolan on May 31, 2012

source : http://net-informations.com/vbprj/communications/internet-connection.htm

Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef conn As Long, ByVal val As Long) As Boolean

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Out As Integer
    If InternetGetConnectedState(Out, 0) = True Then
        MsgBox("Connected !")
    Else
        MsgBox("Not Connected !")
    End If
End Sub

You need to login to post a comment.