开发者

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 o开发者_如何学运维nly 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?


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.


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


I usually expose a Ping web method on my server, which sometimes also does some basic healthchecks of the system (if appropriate). Then its just a matter of a web call to your Ping method and you know the state of your system.


Or you can do a nice Socket connection to the IIS port (default 80). Send a string like "PING\r\n" and see what you get in return.

You would normally get a "HTTP/1.1 400 Bad Request...." msg in return. That way you know its up and running.

If you can not get a connection the web server is either down or something is blocking inbetween (firewalls / router settings etc).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜