Check the URL States Using Ping Command

There will be situation where you need to check some site like they are live or dead...

Then you can use the Ping method of the System.Net.NetworkInformation Class.

This code Ping the Google.Com, If the site is alive you will get the Reply status as Success or Else you will came with the reply of Not Available.


//Import this in the Page Level
//System.Net.NetworkInformation;
Ping ninfo = new Ping();
try
{
PingReply pReply = ninfo.Send("www.google.com");
if (pReply.Status == IPStatus.Success)
{
Response.Write("Ping Reply SuccessFul. Able to Communicate to the server");
}
//Or Print the Message
else
{
Response.Write("Ping Reply Failed. Not Able to Communicate to the server");
}

}
catch (Exception ex)
{
//If Any Exception Occurs. Print the error
Response.Write(ex.Message);
}



Make Sure that the Site Address is Correct , Or else you will get the Exception that the Resource is not a Valid One Reply. This invalid Address will be handled by this part of code



catch (Exception ex)
{
//If Any Exception Occurs. Print the error
Response.Write(ex.Message);
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: