如何检测WPF WebBrowser控件何时无法加载页面

时间:2016-03-11 12:38:04

标签: c# wpf

我需要在System.Windows.Controls.WebBrowser中显示一些页面,如果页面出现问题(服务器关闭,页面不存在,连接断开),控件应隐藏,并且看起来很漂亮的错误消息应该显示“报告问题”提示而不是丑陋的IE页面,并显示错误。

但我无法弄清楚如何检测加载错误。即使服务器不可用,WebBrowser也会引发NavigatedLoadCompleted个事件。即使页面成功显示NavigatedLoadCompleted也会在null的所有NavigationEventArgs属性中引发DECLARE @d1 DATETIME, @d2 DATETIME SELECT @d1 = '2016-03-01' ,@d2= '2016-03-31' ;WITH dates ( [date] ) AS ( SELECT @d1 UNION all SELECT dateadd(d,1,date) FROM dates where date < @d2 ) select DISTINCT DATEPART(week,[date]) AS WeekNumber , ISNULL(M.TotalShipAmt,0) ,ISNULL(M.Total,0) from dates LEFT OUTER JOIN( SELECT SUM([ShipAmt]) AS TotalShipAmt, SUM([Total])AS Total, DATEPART(week,[Cdate]) AS WEEKNUMBER FROM ProductOrders ProductOrders GROUP BY DATEPART(week,[Cdate]) )AS M ON M.WEEKNUMBER=DATEPART(week,[date])

有没有办法确定WebBrowser何时无法加载页面?

1 个答案:

答案 0 :(得分:-1)

管理此方法的一种方法是在使用WebBrowser导航到页面之前执行独立的HTTP GET请求,只是为了检查页面是否可用:

string url = "http://site.domain.com/somepage";
try
{
    WebRequest request = WebRequest.Create(url);
    HttpWebResponse response = (HttpWebResponse) request.GetResponse();

    if (response.StatusCode != HttpStatusCode.OK)
    {
        throw new InvalidOperationException()
    }

    // page is available. Load it to the WebBrowser and show it to the user
    webBrowser.Naviage(url);
}
catch (Exception exception)
{
    // page isn't available - show an error screen
}

<强>更新

我发现的所有其他方法: