Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if website is online with ASP.NET C#?

Tags:

c#

asp.net

I would like to know how to check if a website is offline or online using C#?

like image 900
Marc Avatar asked Oct 18 '25 23:10

Marc


1 Answers

Try to hit the URL using HttpWebClient over an HTTP-GET Request. Call GetResponse() method for the HttpWebClient which you just created. Check for the HTTP-Status codes in the Response.

Here you will find the list of all HTTP status codes. If your request status code is statrting from 5 [5xx] which means the site is offline. There are other codes that can also tell you if the site is offline or unavailable.You can compare the codes against your preferred ones 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();
like image 83
this. __curious_geek Avatar answered Oct 20 '25 12:10

this. __curious_geek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!