如何知道WebBrowser是否导航错误页面?

时间:2009-05-19 03:21:53

标签: c#

例如,我们导航到http://www.ggg111.com,这是一个错误网址。但是在WebBrowser中它会显示一个错误页面:

The webpage cannot be found 
 HTTP 400  
   Most likely causes:
There might be a typing error in the address. 
If you clicked on a link, it may be out of date. 

   What you can try: 
     Retype the address.  

     Go back to the previous page. 
     Go to  and look for the information you want.  

如何知道WebBrowser是否导航错误页面?

还检查一下: https://sso.youshang.com/sso/userAuthnAction.do1

HTTP Status 404 - /sso/userAuthnAction.do1
type Status report
message /sso/userAuthnAction.do1
description The requested resource (/sso/userAuthnAction.do1) is not available.

2 个答案:

答案 0 :(得分:2)

基于此控件:http://www.codeproject.com/KB/cpp/ExtendedWebBrowser.aspx

存在NavigateError函数,在导航期间发生错误时触发。 这是我修改过的代码:

首先添加事件参数类:


public class NavigateErrorArgs : EventArgs
{
    public object StatusCode { get; set; }

    public NavigateErrorArgs()
            : base()
    { }

    public NavigateErrorArgs(object statusCode)
            : base()
    {
       this.StatusCode = statusCode;
    }
}

然后在类ExtendedWebBrowser中添加委托和事件:


public delegate void NavigateErrorHandler(object sender, NavigateErrorArgs e);
public event NavigateErrorHandler NavigateError;

protected void OnNavigateError(NavigateErrorArgs e)
{
  if (NavigateError != null)
      NavigateError(this, e);
}

并修改WebBrowserExtendedEvents类中的方法:


public void NavigateError(object pDisp, ref object URL, ref object frame, ref object statusCode, ref bool cancel)
{
    _Browser.OnNavigateError(new NavigateErrorArgs(statusCode));
}

答案 1 :(得分:-2)

不确定这个,但是...... 试试吧,......

string check = webBrowser1.DocumentText;
        if (check.IndexOf("The webpage cannot be found") > 1)
        {
            MessageBox.Show("ERROR OCCURED");
            //what else you want to do, do here!!!
        } 

这将搜索给定的文本,如果找到则将执行更多代码