打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
重写 WebBrowser 获取 网络连接错误信息
2010-12-02 10:46 1529人阅读 评论(0) 收藏 举报

一下方案可向WebBrowser 注册一个NavigateError方法用于返回连接错误信息,包括网络无法连接、404找不到网页等等错误。

 

1.自定义NavigateError事件的参数:

[c-sharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System;  
  5. using System.Runtime.InteropServices;  
  6. namespace PageDownLoad  
  7. {  
  8.     public class WebBrowserNavigateErrorEventArgs : EventArgs  
  9.     {  
  10.         private String urlValue;  
  11.         private String frameValue;  
  12.         private Int32 statusCodeValue;  
  13.         private Boolean cancelValue;  
  14.         public WebBrowserNavigateErrorEventArgs(  
  15.             String url, String frame, Int32 statusCode, Boolean cancel)  
  16.         {  
  17.             urlValue = url;  
  18.             frameValue = frame;  
  19.             statusCodeValue = statusCode;  
  20.             cancelValue = cancel;  
  21.         }  
  22.         public String Url  
  23.         {  
  24.             get { return urlValue; }  
  25.             set { urlValue = value; }  
  26.         }  
  27.         public String Frame  
  28.         {  
  29.             get { return frameValue; }  
  30.             set { frameValue = value; }  
  31.         }  
  32.         public Int32 StatusCode  
  33.         {  
  34.             get { return statusCodeValue; }  
  35.             set { statusCodeValue = value; }  
  36.         }  
  37.         public Boolean Cancel  
  38.         {  
  39.             get { return cancelValue; }  
  40.             set { cancelValue = value; }  
  41.         }  
  42.     }  
  43. }  
 

 

 

2.扩展webbrowser为MyWebBrowser,在程序中将Webbrowser改成Mywebbrowser

 

[c-sharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Security.Permissions;  
  5. using System.Runtime.InteropServices;  
  6. using System.Windows.Forms;  
  7. namespace PageDownLoad  
  8. {  
  9.     public class MyWebBrowser : WebBrowser  
  10.     {  
  11.         [ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),  
  12.         InterfaceType(ComInterfaceType.InterfaceIsIDispatch),  
  13.         TypeLibType(TypeLibTypeFlags.FHidden)]  
  14.         public interface DWebBrowserEvents2  
  15.         {  
  16.             [DispId(271)]  
  17.             void NavigateError(  
  18.                 [In, MarshalAs(UnmanagedType.IDispatch)] object pDisp,  
  19.                 [In] ref object URL, [In] ref object frame,  
  20.                 [In] ref object statusCode, [In, Out] ref bool cancel);  
  21.         }  
  22.         AxHost.ConnectionPointCookie cookie;  
  23.         MyWebBrowserEventHelper helper;  
  24.         public delegate void WebBrowserNavigateErrorEventHandler(object sender,  
  25.             WebBrowserNavigateErrorEventArgs e);  
  26.   
  27.         [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]  
  28.         protected override void CreateSink()  
  29.         {  
  30.             base.CreateSink();  
  31.             // Create an instance of the client that will handle the event  
  32.             // and associate it with the underlying ActiveX control.  
  33.             helper = new MyWebBrowserEventHelper(this);  
  34.             cookie = new AxHost.ConnectionPointCookie(  
  35.                 this.ActiveXInstance, helper, typeof(DWebBrowserEvents2));  
  36.         }  
  37.         [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]  
  38.         protected override void DetachSink()  
  39.         {  
  40.             // Disconnect the client that handles the event  
  41.             // from the underlying ActiveX control.  
  42.             if (cookie != null)  
  43.             {  
  44.                 cookie.Disconnect();  
  45.                 cookie = null;  
  46.             }  
  47.             base.DetachSink();  
  48.         }  
  49.         public event WebBrowserNavigateErrorEventHandler NavigateError;  
  50.         // Raises the NavigateError event.  
  51.         protected virtual void OnNavigateError(  
  52.             WebBrowserNavigateErrorEventArgs e)  
  53.         {  
  54.             if (this.NavigateError != null)  
  55.             {  
  56.                 this.NavigateError(this, e);  
  57.             }  
  58.         }  
  59.         // Handles the NavigateError event from the underlying ActiveX   
  60.         // control by raising the NavigateError event defined in this class.  
  61.         private class MyWebBrowserEventHelper :  
  62.             StandardOleMarshalObject, DWebBrowserEvents2  
  63.         {  
  64.             private MyWebBrowser parent;  
  65.             public MyWebBrowserEventHelper(MyWebBrowser parent)  
  66.             {  
  67.                 this.parent = parent;  
  68.             }  
  69.             public void NavigateError(object pDisp, ref object url,  
  70.                 ref object frame, ref object statusCode, ref bool cancel)  
  71.             {  
  72.                 // Raise the NavigateError event.  
  73.                 this.parent.OnNavigateError(  
  74.                     new WebBrowserNavigateErrorEventArgs(  
  75.                     (String)url, (String)frame, (Int32)statusCode, cancel));  
  76.             }  
  77.         }  
  78.     }  
  79. }  
 

 

这个时候你的mywebbrowser控件就有了onnavigateerror事件,在这个事件里编写您的错误处理方法。您或许需要用到错误消息中的错误代码,具体错误代码请参照http://msdn.microsoft.com/zh-cn/library/bb268233(en-us,VS.85).aspx里的每个错误代码代表的意义。

本人项目中所使用的错误处理代码如下:

        private void WebBrowserIE_NavigateError(object sender, WebBrowserNavigateErrorEventArgs e)
        {
            log.Debug("ERROR:----------" + e.Url);
            int code = e.StatusCode;
            // 发生错误时,转向本地页面
            if (code == -2146697211)
            {                WebBrowserIE.Navigate("本地页面");
            }
        }

 

转自:http://hi.baidu

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
换用代理IP的Webbrowser方法
Using ActiveX Controls in .NET Applications | The Essentials for Using COM in Managed Code | InformI
C# webBrowser禁止在新窗口打开,强制在本窗口打开
在WebBroswer里面显示Word
webBrowser用法小结
WebBrowser让DocumentCompleted事件执行完毕后再往下执行
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服