WebBrowser is a winndows forms control which allows you to add web browsing capabilities to windows application developed with .net technology(i.e C#.net or vb.net).This control acts in a similar way to Internet Explorer Browser.Its supports methods & events to navigate to url ,properties to work with HTML Document Object Model(DOM) etc.But it doesn't allow to capture directly some things programatically,for example HTTP status codes.If the requested web page is not found then HTTP Status code '404' is shown on web browser,but with if u want capture this status code programatically,its not straight farward. Actually WebBrowser windows forms control is wrapper WebBrowser ActivexControl in 'Microsoft Internet Control(SHDocVw.dll)' library which is a COM component.WebBrowser windows forms control doesnt support all the events supported by Activex Control (Web Browser).Activex Control supports 'NavigateError' event which will be raised when any error occured while navigating to url.By handling this event one can get HTTP Status codes for a requested url.
To capture 'NavigateError' with windows forms 'Web Browser' control a work around is there.Its possible to cast windows forms 'Web Browser' into Activex control.The under lying ActiveXControl of windows forms WebBrowser control can be accessed through a property called 'ActiveXInstance'.The code looks like this
Code :
Following library to added to the project.
* 'Microsoft Internet Control(SHDocVw.dll) which can be found under COM tab of Add Reference window.
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
System.Windows.Forms.WebBrowserBase wb1;
SHDocVw.WebBrowser wb2 = (SHDocVw.WebBrowser)wb.ActiveXInstance;
wb2.NavigateError += new DWebBrowserEvents2_NavigateErrorEventHandlerwb2_NavigateError);
//event handler for NavigateError event
void wb2_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
{
throw new NotImplementedException();
}
If any error occured while navigating to URL the 'NavigateError' will be raised.The 'StatusCode'
parameter of the event handler gives the HTTP Status code.
Note:Not all the HTTP status codes can be get through this approach.As the event is raised only when an error occured while navigating,only HTTP status codes related to error can be accesed.Following are the list of HTTP Status codes that can be accessed.
HTTP_STATUS_BAD_GATEWAY (502)
HTTP_STATUS_BAD_METHOD (405)
HTTP_STATUS_BAD_REQUEST (400)
HTTP_STATUS_CONFLICT (409)
HTTP_STATUS_DENIED (401)
HTTP_STATUS_FORBIDDEN (403)
HTTP_STATUS_GATEWAY_TIMEOUT (504)
HTTP_STATUS_GONE (410)
HTTP_STATUS_LENGTH_REQUIRED (411)
HTTP_STATUS_NONE_ACCEPTABLE (406)
HTTP_STATUS_NOT_FOUND (404)
HTTP_STATUS_NOT_SUPPORTED (501)
HTTP_STATUS_PAYMENT_REQ (402)
HTTP_STATUS_PRECOND_FAILED (412)
HTTP_STATUS_PROXY_AUTH_REQ (407)
HTTP_STATUS_REQUEST_TIMEOUT (408)
HTTP_STATUS_REQUEST_TOO_LARGE (413)
HTTP_STATUS_RETRY_WITH (449)
HTTP_STATUS_SERVER_ERROR (500)
HTTP_STATUS_SERVICE_UNAVAIL (503)
HTTP_STATUS_UNSUPPORTED_MEDIA (415)
HTTP_STATUS_URI_TOO_LONG (414)
HTTP_STATUS_VERSION_NOT_SUP (505)
Subscribe to:
Post Comments (Atom)
5 comments:
see http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.createsink(VS.80).aspx
for an alternative approach.
The has some typos.
The code has some typos.
Cool man.
locked in this a very long time and was checking the html element for any errors.
you made it easy
Thanks for sharing this post. Your post is really very helpful its students. Dot Net Online Training Hyderabad
Post a Comment