Sunday, July 28, 2013

Check Internet Connection availability in ASP. Net



 Check Internet Connection availability in ASP. Net

using System;
using System.Linq;
using System.Net;
namespace CheckNetConnection
{
    class Program
    {
        static void Main(string[] args)
        {
            CheckInternetConnection();
        }
        public static void CheckInternetConnection()
        {
            HttpWebRequest objReq ;
            HttpWebResponse objRes ;
            try
            {
                objReq = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
                objRes = (HttpWebResponse)objReq.GetResponse();
                if (objRes.StatusCode == HttpStatusCode.OK)
                {
                    Console.WriteLine("Congrats ! Internet Connection is Available.");
                }
                else
                {
                    Console.WriteLine("Sorry ! Internet Connection is not Available.");
                }
                Console.ReadKey();
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More