如何检查Web服务器是否启动? (C#)

发布于 2024-09-15 22:16:56 字数 342 浏览 4 评论 0原文

我正在构建一个监控应用程序来持续监控系统的各个方面。我希望使用 Ping() 函数来确定服务器是否已启动,但是 MSDN 文档 本身表示这不是最好的方法:

成功的 Ping 仅表明 可以通过以下方式访问远程主机 网络;更高级别的存在 上的服务(例如 Web 服务器) 不保证远程主机。

还有其他方法可以做得更好吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

女中豪杰 2024-09-22 22:16:57

或者您可以对 IIS 端口(默认 80)进行良好的套接字连接。
发送一个类似“PING\r\n”的字符串,看看你会得到什么回报。

您通常会收到“HTTP/1.1 400 Bad Request....”消息作为回报。这样您就知道它已启动并正在运行。

如果您无法获得连接,则网络服务器可能已关闭,或者中间有某些东西被阻止(防火墙/路由器设置等)。

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).

菊凝晚露 2024-09-22 22:16:56

您需要确定要测试的页面/URL,可以是已经存在的页面/URL,也可以是您为检查服务器可用性的特定目的而创建的页面/URL。

每[一段时间],您都可以向此 URL 发出请求,并检查响应以确定它是否包含预期值,例如,如果您请求 mysite.com/default,则为页面内容。 .aspx.最好的办法是将一个 url/页面/Web 服务调用专用于“ping”,因为它可以确保如果您正在检查的任意页面的内容在您不知情的情况下发生更改,它不会被破坏。

我建议反对做的一件事是使用这一 ping 方法测试一切,包括数据库连接、第 3 方 Web 服务可用性、磁盘空间等,...如果如果您想执行此操作,可以调用多种方法,这样您就可以一目了然地准确了解导致问题的原因。

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.

陌上芳菲 2024-09-22 22:16:56

只需为您的站点创建一个处理程序(或页面),以 xml、json 或任何其他格式返回有关您的系统的一些信息,例如,

database: ok
service x: ok

然后使用 WebClient 检索该信息

var client = new WebClient();
string response = client.DownloadString("http://mydomain.com/state.ashx");
// parse information

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
止于盛夏 2024-09-22 22:16:56

我通常在我的服务器上公开一个 Ping Web 方法,有时还会对系统进行一些基本的运行状况检查(如果适用)。然后,只需对 Ping 方法进行 Web 调用,您就可以知道系统的状态。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文