Check if a site is online using ASP.NET 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();
source to share
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
source to share
If you mean the online / offline state that IIS manages, then you can manage this with custom web events (application lifecycle events)
source to share