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

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.

Leave a Comment