![](/img/trans.png)
[英]Need HTTP Status (200/500/etc) Code from WebBrowser Control
[英]http status code 500 / webpage stops refreshing
我們有一個Intranet網頁,用於顯示信息以在我們站點周圍的顯示器上工作。 該網頁每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);
}
希望有人可以指出我出了問題的地方,或者是做這件事的更好方法,因為目前我們必須手動刷新/重新加載網頁。
您是否完成了整個頁面的重新加載?
您如何使用ajax調用呢? 您可以將其放在setInterval / setTimeout中,然后處理成功/失敗的響應-應該使其能夠抵抗服務失敗,並且更加用戶友好。
是的,您可能應該消除500。Try / catch {msg(“ OMG,無法訪問數據庫!”)}類型的東西:)
嗯,您的代碼存在問題,可能是您的代碼檢查了數據庫及其正常工作,但隨后第500個用戶連接,頁面刷新失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.