Check if a site is online using ASP.NET C #?
I would like to know how to check if a site is offline or online using C #?
Try to push the url with the HttpWebClient on the HTTP GET request. Call the GetResponse () method on the HttpWebClient you just created. Check the HTTP status codes in the response.
Here you will find a list of all HTTP status codes. If your request status code is statrting with 5 [5xx], that means the site is offline. There are other codes that can also tell you if a site is offline or unavailable. You can compare codes with preferred codes from the entire list.
//Code Example
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create("http://www.stackoverflow.com");
httpReq.AllowAutoRedirect = false;
HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
if (httpRes.StatusCode==HttpStatusCode.NotFound)
{
// Code for NotFound resources goes here.
}
// Close the response.
httpRes.Close();
First select "online" and "offline". However, if your code is working, your site is online.
For my web applications I use the "Offline" option, which the administrator can set / disable. Then I can check this setting programmatically. I use this Offline setting to show a friendly service message to my users. Alternatively, you can use App_Offline.htm, Help: http://www.15seconds.com/issue/061207.htm
http://weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx
If you mean the online / offline state that IIS manages, then you can manage this with custom web events (application lifecycle events)
http://support.microsoft.com/kb/893664
http://msdn.microsoft.com/en-us/library/aa479026.aspx
You can use Pingdom.com and its API. Check out the source code for the "Alerter for Pingdom API" at the bottom of this page