none
Best Way to Check Internet Connection (Asked Again) RRS feed

  • Question


  • 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: http://www.google.com/

    '''''sWebsite = "http://www.google.com/"

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

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.devices.network.isavailable?view=netframework-4.7.2


    What can I do to check Internet Conncetion?

    ADawn


    ADawn



    Monday, February 25, 2019 4:02 PM

All replies

  • If you ask questions containing the word "Best" and nothing more, then you can ask this probably endless times. 

    Nobody knows what in your opinion is "Best". 

    If it is as example about sport I like the most basketball. But that does not mean it is the "best" sport. 

    Therefore tell what you want, and don't ask questions like what is the "best" way to see sport on television.


    Success
    Cor

    Monday, February 25, 2019 4:58 PM
  • What can I do to check Internet Conncetion?


    I gave you the methods I used and that worked in a Freeware...
    Monday, February 25, 2019 5:06 PM
  • Okay... 

    I can't use PING.

    I can't use HttpWebResponse because it doesn't catch the exception 75% of the time.

    I can't use IsAvailable because my application is a ClickOnce.

    So, I'm asking for away to check Internet connection that works (hopefully) 100% of the time.

    Thanks,

    ADawns 


    ADawn

    Monday, February 25, 2019 5:08 PM
  • Have you tested the above answer with IsInternetConnected and so on ?
    Monday, February 25, 2019 5:22 PM
  • As explained in my first post, isAvailable (Network.IsAvailable) can't be used according to Microsoft because my application is a ClickOnce.

    Somebody out there must be using something solid to check for Internet. What do web browsers (Chromo, FoxFire, etc.) use when there search a site?  

    Thanks.



    ADawn

    Monday, February 25, 2019 5:32 PM
  • IsInternetConnected  is not isAvailable

    (I posted the P/Invoke in your other thread)

    • Edited by Castorix31 Monday, February 25, 2019 5:46 PM
    Monday, February 25, 2019 5:39 PM
  • Castorix31

    Where are the methods?

    ADawn


    ADawn

    Monday, February 25, 2019 5:40 PM
  • Okay... 

    I can't use PING.

    I can't use HttpWebResponse because it doesn't catch the exception 75% of the time.

    I can't use IsAvailable because my application is a ClickOnce.

    So, I'm asking for away to check Internet connection that works (hopefully) 100% of the time.

    Thanks,

    ADawns 


    ADawn

    Why can't you use Ping. If you tell that, it becomes if that is well described maybe possible to give an answer for your problem. 

    Be aware, nobody can prevent that your provider shut your connection down because you did not pay. 


    Success
    Cor


    Monday, February 25, 2019 5:40 PM
  • Cor,

    The large company will not allow pings from any computers. 

    ADawn


    ADawn

    Tuesday, February 26, 2019 1:09 AM
  • Hi tropicwhisper,

    I use this code for checking if internet connection is available. Please try.
    ' --- check for internet connection
    Private Sub prc_Check_Connection()
    	If IsInternetConnected() = True Then
    		' --- MessageBox.Show("internet connection available", "connection OK")
    	Else
    		' --- no internet connection: confirm to continue ---
    		While Conneted_Yes = False
    			If MessageBox.Show("no internet connection or taking time. Will you retry?", "Task Clock", _
    							   MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
    				' --- retry
    				System.Threading.Thread.Sleep(2000)
    				If IsInternetConnected() Then
    					Conneted_Yes = True
    				End If
    			Else
    				' --- no retry: terminate application
    				Environment.Exit(0)
    			End If
    		End While
    	End If
    End Sub
    
    ' --- check if internnet connection availabel: using HTTP-HEAD method (return code: True = connected)
    Public Shared Function IsInternetConnected() As Boolean
    	Dim str_URL = "http://hokusosha.com"
    	Dim webreq As System.Net.HttpWebRequest = Nothing
    	Dim webres As System.Net.HttpWebResponse = Nothing
    	' ---
    	Try
    		webreq = CType(System.Net.WebRequest.Create(str_URL), System.Net.HttpWebRequest)
    		' --- HttpWebRequestの作成
    		webreq.Method = "HEAD"
    		webres = CType(webreq.GetResponse(), System.Net.HttpWebResponse)
    		' --- MessageBox.Show("webres = " & webres.StatusCode, "webres")
    		Return True
    	Catch
    		Return False
    	Finally
    		If Not (webres Is Nothing) Then
    			webres.Close()
    		End If
    	End Try
    End Function
    
    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Tuesday, February 26, 2019 2:27 AM
  • Cor,

    The large company will not allow pings from any computers. 

    ADawn


    ADawn

    Ah you are not looking if your computer is connected to Internet. You are looking if a website is available. 

    A complete different question. 

    Try to describe what you want, instead of describing what you don't want. 

    This forum works with a point system, therefore you get always a reply and if not even from a Microsoft subtracter employee who asks you to mark the reply as answer.


    Success
    Cor


    • Edited by Cor Ligthert Tuesday, February 26, 2019 11:39 AM
    Tuesday, February 26, 2019 11:37 AM
  • You stated that, "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."  Can you give an example of a bogus website that doesn't throw an exception?

    Here is a mod to your code that adds a DNS check.

            Const iTimeout_HTTP As Int16 = 5000
            Dim success As Boolean = False
    
            Dim sWebsite As String = "http://dsjjjsbdcjhss.com"
            'sWebsite = "http://www.google.com/"
    
            Dim http_request As System.Net.HttpWebRequest
            Dim http_response As System.Net.HttpWebResponse
            http_request = DirectCast(System.Net.HttpWebRequest.Create(sWebsite), System.Net.HttpWebRequest)
            Dim hostInfo As Net.IPHostEntry
    
            Try
                hostInfo = Net.Dns.GetHostEntry(http_request.Host) 'host name resolve?
                http_request.Timeout = iTimeout_HTTP
                http_response = DirectCast(http_request.GetResponse(), System.Net.HttpWebResponse)
    
                If http_response.StatusCode = System.Net.HttpStatusCode.OK Then
                    success = True
                Else
                    success = False
                End If
                http_response.Dispose()
            Catch scktEX As Net.Sockets.SocketException
                Stop 'no such host
            Catch ex As Exception
                Stop '???
            End Try
            http_request = Nothing
            http_response = Nothing
    


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein


    • Edited by dbasnett Tuesday, February 26, 2019 4:49 PM
    Tuesday, February 26, 2019 1:35 PM
  • What IsInternetConnected does is really just connecting to a test web site setup by Microsoft

    You can send web request to the same Microsoft website. 

    If the user does not have NetworkInformationPermission i am not sure what you can do in case the user lost internet connectivity though. 



    Visual C++ MVP

    Tuesday, February 26, 2019 8:09 PM