為了要讓WEBBROWSER能夠追到網頁的ERROR CODE查了好多資料,都沒結果,好不容易找到了MSDN,卻還是看不懂,不過MSDN的好處是稍稍理解一下,就可以直接剪程式碼來用了,我把程式碼做作CLASS,只要讓WEBBROWSER實做WEBBROWSER2即可。(也是要稍稍動點手腳啦)
在Form1.designer.cs裡面要改成
this.xwebbowser = new WebBrowser2();
然後手動寫事件發生時要做什麼
private void wb_NavigateError(object sender, WebBrowserNavigateErrorEventArgs e)
{
console.writeline(e.StatusCode);
}
這裡要注意的是事件的委派要從『屬性』視窗的『事件』頁面的"其他"裡指定事件
這樣e.StatusCode就能帶出錯誤碼,EX:找不到頁面的http 404的那個404碼
msdn裡有列錯誤碼所代表的意思
http://msdn.microsoft.com/en-us/library/bb268233(VS.85).aspx
CLASS 如下
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace webHeartBeat_checker
{
//繼承webbrowser
public class WebBrowser2 : WebBrowser
{
AxHost.ConnectionPointCookie cookie;
WebBrowser2EventHelper helper;
[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 WebBrowser2EventHelper(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 WebBrowser2EventHelper : StandardOleMarshalObject, DWebBrowserEvents2
{
private WebBrowser2 parent;
public WebBrowser2EventHelper(WebBrowser2 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));
}
}
}
// Represents the method that will handle the WebBrowser2.NavigateError event.
public delegate void WebBrowserNavigateErrorEventHandler(object sender, WebBrowserNavigateErrorEventArgs e);
// Provides data for the WebBrowser2.NavigateError event.
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; }
}
}
// Imports the NavigateError method from the OLE DWebBrowserEvents2
// interface.
[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);
}
}
WebBrowserExtensibility.rar