How do you check if a website is online in C#?

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;

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.

Tags:

C#