0

WE have an intranet webpage which is used to display information to works on monitors around our site. This webpage is automatically refreshed every 15 seconds.

All this works find, until the Database server has a problem and the webpage and no longer get a connection, and we get an error back normally an HTTP 500 error.

My solution to this has been to write a C# application that checks the HTTP status of the webpage, and if a HTTP 500 is found to close the browser and then reopen it again and display the webpage. This application is using a timer event set to ever 30 seconds.

The problem I am having is my C# application does not always pick up the 500 error, or any other error that may cause the webpage to stop refreshing.

Below is the code I have written to try and check for the error(s)

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();

            }
        }
    }
}

The webpage is currently loaded from within the application using

public static void Start_webpage()
{
    startInfo.WindowStyle = ProcessWindowStyle.Normal;
    startInfo.FileName = "IExplore.exe";
    startInfo.Arguments = txt_url;
    someProcess = Process.Start(startInfo);
}

Hoping someone can point out where I have gone wrong, or a better way of doing this, as currently we are have to manually refresh/reload the webpage.

4

1 回答 1

0

您是否进行了完整的页面重新加载?

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

是的,您可能应该消除 500。Try/catch{msg("OMG,数据库不可访问!")} 类型的东西 :)

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

于 2013-04-30T20:22:15.550 回答