HTTP状态码500 /网页停止刷新

本文关键字:刷新 网页 状态 HTTP | 更新日期: 2023-09-27 18:11:39

我们有一个内部网网页,用于在我们网站周围的监视器上显示信息。此网页每隔15秒自动刷新一次。

所有这些工作发现,直到数据库服务器有问题,网页和不再获得连接,我们得到一个错误返回通常是HTTP 500错误。

我的解决方案是编写一个c#应用程序,检查网页的HTTP状态,如果发现HTTP 500关闭浏览器,然后重新打开浏览器并显示网页。此应用程序使用的计时器事件设置为每30秒一次。

我遇到的问题是我的c#应用程序并不总是拾取500错误,或任何其他可能导致网页停止刷新的错误。

下面是我写的代码,试图检查错误

public static void Check_Process()
{
    Console.Write("checking started at {0}" + Environment.NewLine, DateTime.Now);
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txt_url);
        response = (HttpWebResponse)request.GetResponse();
        // Read the error stream first and then wait.
        string error = someProcess.StandardError.ReadToEnd();

        Console.Write(error);
    }
    catch (WebException e)
    {
        if (e.Status == WebExceptionStatus.ProtocolError)
        {
            response = (HttpWebResponse)e.Response;
            if ((int)response.StatusCode == 500)
            {
                Console.Write((int)response.StatusCode + " error found at {0}" + Environment.NewLine, DateTime.Now);
                Close_webpage();
                Start_webpage();
            }
            else if (response.StatusCode != HttpStatusCode.OK)
            {
                Console.Write((int)response.StatusCode + " error found at {0}" + Environment.NewLine, DateTime.Now);
                Close_webpage();
                Start_webpage();
            }
        }
    }
}

当前使用

从应用程序内部加载网页。
public static void Start_webpage()
{
    startInfo.WindowStyle = ProcessWindowStyle.Normal;
    startInfo.FileName = "IExplore.exe";
    startInfo.Arguments = txt_url;
    someProcess = Process.Start(startInfo);
}

希望有人能指出我在哪里做错了,或者这样做的更好的方式,因为目前我们必须手动刷新/重新加载网页。

HTTP状态码500 /网页停止刷新

是否重新加载整个页面?

如何使用ajax调用代替?你可以把它放在setInterval/setTimeout中,然后处理成功/失败的响应-应该使它能够抵抗服务失败,并且更加用户友好。

是的,你可能应该去掉500。尝试/捕获{msg("OMG,数据库不可访问!")}类型的东西:)

啊,你的代码的问题可能是你的代码检查数据库,它很好,但然后第500个用户连接,你的页面刷新失败。