c# - 我的网站是否已关闭、无法正常工作或出现错误?

标签 c# .net asp.net vb.net web

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

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

最佳答案

使用 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 事件或其他事件以定期进行检查。

关于c# - 我的网站是否已关闭、无法正常工作或出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/607846/

相关文章:

c# - 如何在我的数据库中创建对用户系统中 ASP.NET 构建的用户的引用?

c# - 单元测试项目 Azure Functions 无法加载文件或程序集 'Newtonsoft.Json'

c# - 如何显示来自两个不同表的日期?

c# - 使用c#获取两个不同对象的公共(public)属性

c# - TabControl 取消选项卡的更改

ASP.Net Azure Web 应用程序身份验证重定向到域而不是本地主机

c# - 检测光驱中的 cd/dvd 并在窗口媒体播放器上播放

.net - 如何将默认字符串编码更改为 UTF 8?

.net - 确定泛型参数是否为 Nullable 类型

asp.net - 保护从 DMZ 到企业域中 WCF 服务的服务调用