Checking Internet Connection

So basically there are 3 ways to do it. We will check each of them and will find the best out of them.

 1) Send WebRequest

                private static bool IsInternetConnected()
        {
            HttpWebRequest req;
            HttpWebResponse resp;
            bool bRet = false;
            try
            {
                req = (HttpWebRequest) WebRequest.Create("
http://www.google.com");
                resp = (HttpWebResponse) req.GetResponse();
                bRet = resp.StatusCode.ToString().Equals("OK");
                resp.Close();
            }
            catch (Exception exc)
            {
                bRet = false;
            }
            finally
            {
                // resp.Close();
            }
            return bRet;
        }

Remarks : You need System.Net namespace to use HttpWebrequest and HttpWebresponse.

 

2) InternetGetConnectedState()  – wininet api

 

       private static bool APIInternetGetConnectedState()
        {
            bool bRet = false;
            try
            {
                int Desc = 0;
                bRet = InternetGetConnectedState(ref Desc, 0);
            }
            catch (Exception exc)
            {
                bRet = false;
            }
            return bRet;
        }

Remarks : You need to create extern function to use this api and have to use runtime interop assemblies. So you need to reference namespace  

        System.Runtime.InteropServices and include following code in your class definition.

        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(ref int description, int ReservedValue);

 

3 )InternetCheckConnection – wininet api

        private static bool APIInternetCheckConnection()
        {
            bool bRet = false;
            try
            {
                bRet = InternetCheckConnection("
http://www.google.com", 0x01, 0);
            }
            catch (Exception exc)
            {
                bRet = false;
            }
            return bRet;
        }

Remarks : You need to create extern function to use this api and have to use runtime interop assemblies. So you need to reference namespace  

        System.Runtime.InteropServices and include following code in your class definition.

        [DllImport("wininet.dll")]
        private extern static bool InternetCheckConnection(string description,int flags, int ReservedValue);

 

Comparision :

Table shown below compares all the methods. Interestingly InternetGetConnectedState() reports connected even if we are not connected to Internet, and that is very natural since it has no way to detect if you are connected to actual Internet ( ie particular server ) unless you send a request to web server. You can find a good explanation about it here.Again you have to use interop services to use both this api which can cause performance issues.

Using Google server as server to request has also pros and cons. 1) Google is used heavily – but again it effectively uses CDN, so I don't think this argument holds a point. 2) You are almost very sure that Google server will be up and running in almost all circumstances, but yet for the sake of precaution use this as parameter and get it from either Database or App.Config

So after all of this for the time being I will go with Method 1 ie by using Send Web Request.

Method No Connection Connected to Intranet Connected to Internet
Send WebRequest false (0 ms) false (0 ms) true (550 ms)
InternetGetConnectedState false ( 30 ms ) true ( 0 ms ) true ( < 10 ms )
InternetCheckConnection false ( more than 2 seconds ) false ( 400 ms ) true ( 78 ms )

 

There exist other methods also to check Internet connectivity ie Create a socket connection to Web server, Use Ping or tracert, Open a telnet connection but many of this can create problems when firewall is on. yet, I am still interested in finding a better method than this. Please let me know if you know one.

 

Best Regards,

Mahin Gupta