Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

I want my program in C# to check if a website is online prior to executing, how would I make my program ping the website and check for a response in C#?

like image 625
Alper Avatar asked Sep 23 '11 02:09

Alper


1 Answers

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.

like image 133
Digicoder Avatar answered Sep 22 '22 21:09

Digicoder