There are different ways to check if the internet connection is available. Below are few simple ways to do it.

System.Net Namespace offers different classes to acheive that.

WebRequest, WebResponse, HttpWebRequest and HttpWebResponse classes can be used to call a website and get response back.

In the above code, if internet or the url is not accessible, request.GetResponse() will throw an exception.

WebClient is another class that can be used to send and receive data from website or other resources accessed via a URI.

If internet or uri address is not available, “client.DownloadString(url)” will throw an exception. This method does not care how webpage behaves. Webpage may send error message and the ping method will still return positive.

We can take further step to get specific response by accessing a text file with certain keyword written like “Success”.

Above methods checks for resources available by URI.

NetworkInterface class, avalaible in System.Net.NetworkInformation Namespace, can be used to check if network connection is available. This could be any network including LAN.




Happy Coding…


Check if Internet Connection is available.
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *