Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Web server is up? (C#)

I am building a monitoring application to continuously monitor all aspects of my system. I was hoping to use the Ping() function to determine if the server is up but the MSDN documentation itself says that it is not the best way:

A successful Ping indicates only that the remote host can be reached on the network; the presence of higher level services (such as a Web server) on the remote host is not guaranteed.

Are there any other ways to do this better?

like image 778
Hari Menon Avatar asked Dec 21 '22 23:12

Hari Menon


2 Answers

Just create a handler (or a page) for your site which returns some information about your system in xml, json or any other format e.g.

database: ok
service x: ok

Then use WebClient to retrieve that information

var client = new WebClient();
string response = client.DownloadString("http://mydomain.com/state.ashx");
// parse information
like image 27
Mike Koder Avatar answered Jan 04 '23 05:01

Mike Koder


You'll need to settle on one page/url that you wish to test, either one that's already there or one that you create for the specific purpose of checking the servers availability.

Once every [period of time] you can make a request to this url and examine the response to determine if it contain the expected value(s), such as the content of a page if you requested mysite.com/default.aspx. The best thing to do is to dedicate one url/page/web service call to the "ping" as it ensures that it doesn't get broken if the content of the arbitary page you're checking is changed without you being made aware.

One thing I do recommend against doing is having this one ping method test everything, including your database connectivity, 3rd party web service availability, disk space, etc,... If you want to do this, have multiple methods that you can call so you can see at a glance precisely what's causing the problem.

like image 177
Rob Avatar answered Jan 04 '23 06:01

Rob