我的网站是否已关闭、无法运行或出现错误?

发布于 2024-07-14 02:06:53 字数 150 浏览 9 评论 0原文

我想创建一个小型 Windows 应用程序,它将在每个时间段自动访问我的站点,并检查其运行是否正常,是否发现它已关闭、无法工作或出现错误“示例:404、网络错误、与数据库的连接失败”将在我的屏幕上显示一条消息。

我如何知道使用任何 .NET 语言以编程方式存在错误?

I want to create a small windows application will go automatically every time period to my site and check if its running fine, if it found it down, not working or have an error "Examples: 404, network error, connection to db failed" it will show a message on my screen.

How can i know that there is an error there programmaticly using any .NET language?

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

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

发布评论

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

评论(8

赤濁 2024-07-21 02:06:53

使用 WebClient 可以很容易地做到这一点。 它看起来像这样:

    WebClient client = new WebClient();
    try
    {
        string response =
            client.DownloadString("http://www.example.com/tester.cgi");

        // We at least got the file back from the server

        // You could optionally look at the contents of the file
        // for additional error indicators      
        if (response.Contains("ERROR: Something"))
        {
            // Handle
        }
    }
    catch (WebException ex)
    {
        // We couldn't get the file.
        // ... handle, depending on the ex
        //
        // For example, by looking at ex.Status:
        switch (ex.Status)
        {
            case WebExceptionStatus.NameResolutionFailure:
                // ...
            break;
            // ...
        }
    }

您可以将其与 TimerTick 事件或其他事件挂钩以定期进行检查。

It's pretty easy to do with a WebClient. It would look something like this:

    WebClient client = new WebClient();
    try
    {
        string response =
            client.DownloadString("http://www.example.com/tester.cgi");

        // We at least got the file back from the server

        // You could optionally look at the contents of the file
        // for additional error indicators      
        if (response.Contains("ERROR: Something"))
        {
            // Handle
        }
    }
    catch (WebException ex)
    {
        // We couldn't get the file.
        // ... handle, depending on the ex
        //
        // For example, by looking at ex.Status:
        switch (ex.Status)
        {
            case WebExceptionStatus.NameResolutionFailure:
                // ...
            break;
            // ...
        }
    }

You could hook that up to a Timer's Tick event or something to periodically make the check.

想念有你 2024-07-21 02:06:53

何必呢? 您可以从像 RedAlert 这样的提供商那里以便宜的价格获得更好的解决方案,

这样做的好处是:

1)它从防火墙外部测试您的站点,因此可以检测到更广泛的问题。

2) 它是公正的第三方,因此如果您需要 SLA,您可以证明正常运行时间。

3) 只需支付少量费用,您就可以让该设备尝试并诊断问题。

4) 当出现问题时,它可以给您寻呼或发送电子邮件。

5) 您无需调试新服务器。

天哪,我听起来像是为这些家伙做的广告,但我保证我不会为他们工作或获取回扣。 我刚刚对我们服务器的服务感到满意。

顺便说一句:我查了一下定价,每个网站每月大约 20 美元。 因此,您可能可以比自己构建服务所需的时间更短地支付一年的服务费用。

Why bother? You can get a much better solution for cheap from a provider like RedAlert

The nice thing about this is:

1) It tests your site from outside your firewall, so it can detect a wider variety of problems.

2) It is an impartial 3rd party so you can prove uptime if you need to for an SLA.

3) For a small premium you can have the thing try and diagnose the problems.

4) It can page or e-mail you when there is a problem.

5) You don't need to commission a new server.

Geez, I sound like an ad for the guys, but I promise I don't work for them or get a kickback. I have just been happy with the service for our servers.

BTW: I checked pricing and it is about $20 per site/month. So you could probably pay for a year of the service in less time than it will take to build it yourself.

暮年 2024-07-21 02:06:53

为了执行相同的功能,我首先研究了第三方解决方案。 一项免费且相当准确的特定服务是 MonitorUs

但是,如果您想构建自己的,那么我有一个建议。 考虑使用 Head 请求而不是 get 请求:

HEAD 方法与 GET 相同
除了服务器不得返回
响应中的消息正文。 这
HTTP 中包含的元信息
响应 HEAD 请求的标头
应与信息相同
响应 GET 请求而发送。
该方法可用于获取
有关实体的元信息
请求暗示没有
转移实体本身。
该方法常用于测试
超文本链接的有效性,
可访问性和最近的
修改。 w3.org

这是Peter Bromberg 的文章的链接,该文章解释了如何在 C# 中执行 Head 请求。

Wanting to perform the same functionality I first looked into third party solutions. One particular service that is free and has been fairly accurate is MonitorUs.

If, however, you are wanting to build your own then I would have one recommendation. Consider using a Head request instead of a get request:

The HEAD method is identical to GET
except that the server MUST NOT return
a message-body in the response. The
metainformation contained in the HTTP
headers in response to a HEAD request
SHOULD be identical to the information
sent in response to a GET request.
This method can be used for obtaining
metainformation about the entity
implied by the request without
transferring the entity-body itself.
This method is often used for testing
hypertext links for validity,
accessibility, and recent
modification. w3.org

Here's a link to Peter Bromberg's article that explains how to perform a Head request in C#.

假情假意假温柔 2024-07-21 02:06:53

使用 System.Net.WebClient 对象。 它比 HttpWebRequest 更容易使用。 它有一个“DownloadString”方法,可以将 URL 的内容下载到字符串中。 如果服务器返回 500,该方法也可能抛出 WebException 错误。对于其他错误,您可以解析字符串并查找关键字。

Use the System.Net.WebClient object. It's easier to use than HttpWebRequest. It has a "DownloadString" method that will download the contents of a URL into a string. That method may also throw a WebException error if the server returns a 500. For other errors you can parse the string and look for key words.

陪你搞怪i 2024-07-21 02:06:53

使用 HttpWebRequest,并将其包装在 WebException 的 try catch 中。 异常对象中的错误代码将为您提供代码。 404等。如果是500,则可以打印该消息。

Use HttpWebRequest, and wrap it in a try catch for WebException. The error code in the exception object will give you the code. 404, etc. If it is 500, you could print the message.

舟遥客 2024-07-21 02:06:53

如果这样做,请创建一个特殊页面来执行任何特殊子系统,例如数据库、文件 IO 等,并以纯文本(而不是 html)提供结果。 这将使您能够更轻松地解析返回的数据,并且还将捕获诸如 DB 或 IO 问题之类的问题,这些问题可能不会给您带来 404 或 500 HTTP 错误。

If you do this, create a special page that exercises any special subsystems, like the data base, file IO, etc, and serves up the results in plain text, not html. This will allow you to parse the returned data easier, and will also catch things like DB or IO problems that may not give you a 404 or 500 HTTP error.

心房的律动 2024-07-21 02:06:53

尝试 Adventnet 的应用程序管理器 (http://www.manageengine.com/products/applications_manager/ ),它免费提供 5 个监视器,并提供出色的监控功能

您可以配置在发生故障时可以执行的操作,例如发送电子邮件等。

Try Adventnet's Application Manager (http://www.manageengine.com/products/applications_manager/), it is free for 5 monitors, and provides excellent monitoring capabilities

You could configure the actions that can be done in case of a failure like send email etc.

不弃不离 2024-07-21 02:06:53

如果您希望在网站关闭时收到电子邮件/短信,请尝试我的网站是否正常运行网页基于解决方案。

If you'd prefer to get email/SMS when your sites are down, try the Are My Sites Up web-based solution.

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