重写 WebBrowser 获取 网络连接错误信息
2010-12-02 10:46
183 查看
一下方案可向WebBrowser 注册一个NavigateError方法用于返回连接错误信息,包括网络无法连接、404找不到网页等等错误。
1.自定义NavigateError事件的参数:
2.扩展webbrowser为MyWebBrowser,在程序中将Webbrowser改成Mywebbrowser
这个时候你的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.com/zhxf5210000/blog/item/d2d4e260aa5d82d48cb10d83.html
1.自定义NavigateError事件的参数:
using System; using System.Collections.Generic; using System.Text; using System; using System.Runtime.InteropServices; namespace PageDownLoad { public class WebBrowserNavigateErrorEventArgs : EventArgs { private String urlValue; private String frameValue; private Int32 statusCodeValue; private Boolean cancelValue; public WebBrowserNavigateErrorEventArgs( String url, String frame, Int32 statusCode, Boolean cancel) { urlValue = url; frameValue = frame; statusCodeValue = statusCode; cancelValue = cancel; } public String Url { get { return urlValue; } set { urlValue = value; } } public String Frame { get { return frameValue; } set { frameValue = value; } } public Int32 StatusCode { get { return statusCodeValue; } set { statusCodeValue = value; } } public Boolean Cancel { get { return cancelValue; } set { cancelValue = value; } } } }
2.扩展webbrowser为MyWebBrowser,在程序中将Webbrowser改成Mywebbrowser
using System; using System.Collections.Generic; using System.Text; using System.Security.Permissions; using System.Runtime.InteropServices; using System.Windows.Forms; namespace PageDownLoad { public class MyWebBrowser : WebBrowser { [ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch), TypeLibType(TypeLibTypeFlags.FHidden)] public interface DWebBrowserEvents2 { [DispId(271)] void NavigateError( [In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL, [In] ref object frame, [In] ref object statusCode, [In, Out] ref bool cancel); } AxHost.ConnectionPointCookie cookie; MyWebBrowserEventHelper helper; public delegate void WebBrowserNavigateErrorEventHandler(object sender, WebBrowserNavigateErrorEventArgs e); [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")] protected override void CreateSink() { base.CreateSink(); // Create an instance of the client that will handle the event // and associate it with the underlying ActiveX control. helper = new MyWebBrowserEventHelper(this); cookie = new AxHost.ConnectionPointCookie( this.ActiveXInstance, helper, typeof(DWebBrowserEvents2)); } [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")] protected override void DetachSink() { // Disconnect the client that handles the event // from the underlying ActiveX control. if (cookie != null) { cookie.Disconnect(); cookie = null; } base.DetachSink(); } public event WebBrowserNavigateErrorEventHandler NavigateError; // Raises the NavigateError event. protected virtual void OnNavigateError( WebBrowserNavigateErrorEventArgs e) { if (this.NavigateError != null) { this.NavigateError(this, e); } } // Handles the NavigateError event from the underlying ActiveX // control by raising the NavigateError event defined in this class. private class MyWebBrowserEventHelper : StandardOleMarshalObject, DWebBrowserEvents2 { private MyWebBrowser parent; public MyWebBrowserEventHelper(MyWebBrowser parent) { this.parent = parent; } public void NavigateError(object pDisp, ref object url, ref object frame, ref object statusCode, ref bool cancel) { // Raise the NavigateError event. this.parent.OnNavigateError( new WebBrowserNavigateErrorEventArgs( (String)url, (String)frame, (Int32)statusCode, cancel)); } } } }
这个时候你的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.com/zhxf5210000/blog/item/d2d4e260aa5d82d48cb10d83.html
相关文章推荐
- C# 通过扩展WebBrowser捕获网络连接错误信息
- C# 通过扩展WebBrowser捕获网络连接错误信息
- C# 通过扩展WebBrowser捕获网络连接错误信息
- vm网络连接错误信息:failed to connect virtual device ethernet
- 获取网络适配器的相关信息(包括网络连接名称)
- 获取Android网络连接信息
- Oracle数据库的网络连接配置以及一些常见错误信息解决方法
- 获取网络连接信息
- Android 获取当前网络连接的类型信息
- Android获取应用信息判断网络连接返回桌面及卸载apk结束进程等的实现思路
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第十七节 设置TCP连接状态
- Android系统信息获取 之六:网络连接状态信息获取
- Python绝技笔记--------python操作注册表来获取之前连接的网络信息
- C# 获取适配器网络连接IP地址,子网掩码,DNS,数据包等信息
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第四节 获取当前TCP连接情况
- 【Window】获取电脑连接的网络信息
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第六节 获取当前UDP连接情况
- Android系统信息获取 之六:网络连接状态信息获取
- IOS模拟器能获取运营商信息,网络连接类型以及地理位置吗?