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.

References:

System.Net: http://msdn.microsoft.com/en-us/library/btdf6a7e.aspx

System.Net.NetworkInformation: http://msdn.microsoft.com/en-us/library/3ew4thdx.aspx

Happy Coding…

 

Check if Internet Connection is available.
Tagged on:     

Leave a Reply

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


× seven = 49

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">