您的位置:首页 > 理论基础 > 计算机网络

重写 WebBrowser 获取 网络连接错误信息

2010-12-02 10:46 183 查看
一下方案可向WebBrowser 注册一个NavigateError方法用于返回连接错误信息,包括网络无法连接、404找不到网页等等错误。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: