42

I want my program in C# to check if a website is online prior to executing, how would I make my program ping the website and check for a response in C#?

Alper
  • 1
  • 12
  • 39
  • 78
  • 5
    Has both synchronous and asynchronous examples on the [Ping Class](http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx) page on MSDN. – drew010 Sep 23 '11 at 02:49
  • 2
    You can also use WebClient class and request a resource or a url. Ping doesn't necessarily mean the website is online, unless you just want to check that the server is up. – lahsrah Sep 23 '11 at 02:55
  • That also could mean that your machine is offline though. Alternatively you can use a web site like http://doj.me/ – BrokenGlass Sep 23 '11 at 02:56
  • I went with checking if the server is online, because my program only needs to check for the server being online, not the website, sorry if I worded my question poorly. – Alper Sep 23 '11 at 04:02

5 Answers5

68

A Ping only tells you the port is active, it does not tell you if it's really a web service there.

My suggestion is to perform a HTTP HEAD request against the URL

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("your url");
request.AllowAutoRedirect = false; // find out if this site is up and don't follow a redirector
request.Method = "HEAD";
try {
    response = request.GetResponse();
    // do something with response.Headers to find out information about the request
} catch (WebException wex)
{
    //set flag if there was a timeout or some other issues
}

This will not actually fetch the HTML page, but it will help you find out the minimum of what you need to know. Sorry if the code doesn't compile, this is just off the top of my head.

Digicoder
  • 1,835
  • 1
  • 12
  • 21
60

You have use System.Net.NetworkInformation.Ping see below.

var ping = new System.Net.NetworkInformation.Ping();

var result = ping.Send("www.google.com");

if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
    return;
Korich
  • 1,264
  • 1
  • 10
  • 14
  • 12
    Ping does not tell you whether a website is online, only whether it's network stack is responding. What's more, you're passing an HTML URL to ping, which would not be legal. – Erik Funkenbusch Sep 23 '11 at 03:21
  • 1
    This is by far the best answer, and seeing as the class is even called "Ping", its probably the most correct answer as well. Thank you for your help. Also, if anyone is reading this in the future, here's the link to the manual: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx – Alper Sep 23 '11 at 03:43
  • As for me it doesnt work - it throws an exception (when host is unavailable) rather than simply handling it and returning a corresponding IPStatus value – Laserson Feb 08 '14 at 13:46
  • 7
    As a lot of corporate servers block ping this is not a great method of detecting whether as site is on line. – davehay Feb 06 '15 at 07:50
  • Doesn't work for me when trying to access a legitimate local service: http://localhost:port – Mugen Apr 24 '18 at 13:21
  • This only tells you if a server responds to ping or not. In no way is it an indication if the website is online or not. And yes, there are some servers out there configured to drop ping requests. – waka Jan 22 '21 at 14:09
23

Small remark for Digicoder's code and complete example of Ping method:

private bool Ping(string url)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Timeout = 3000;
        request.AllowAutoRedirect = false; // find out if this site is up and don't follow a redirector
        request.Method = "HEAD";

        using (var response = request.GetResponse())
        {
            return true;
        }
    }
    catch
    {
        return false;
    }
}
uzrgm
  • 375
  • 3
  • 8
9
if (!NetworkInterface.GetIsNetworkAvailable())
{
    // Network does not available.
    return;
}

Uri uri = new Uri("http://stackoverflow.com/any-uri");

Ping ping = new Ping();
PingReply pingReply = ping.Send(uri.Host);
if (pingReply.Status != IPStatus.Success)
{
    // Website does not available.
    return;
}
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
1

The simplest way I can think of is something like:

WebClient webClient = new WebClient();
byte[] result = webClient.DownloadData("http://site.com/x.html");

DownloadData will throw an exception if the website is not online.

There is probably a similar way to just ping the site, but it's unlikely that the difference will be noticeable unless you are checking many times a second.

Matt Fisher
  • 213
  • 1
  • 9
  • 11
    You shouldn't have to download the whole page just to verify that the site is up – BrokenGlass Sep 23 '11 at 02:58
  • The webclient is working better for me instead of the httpwebrequest, after running into error 503, the httpwebrequest keeps throwing 503 while webclient is working again. – bert Oct 06 '17 at 09:29