3

I needed to get http status code from page loaded in the WebBrowser, I ended up with this solution:

I'm using NavigateError event from a WebBrowser ActiveXInstance instance. But it doesn't work properly: I get only a status code in case of an error (obivous, like method name does suggets) if the page can't be loaded and the user wb.Refresh() it and load is OK and I have only the old http status error code stored because successfully load doesn't change my http status code. How do I solve this?

public doSomething()
{
    SHDocVw.WebBrowser axBrowser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
    axBrowser.NavigateError += new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(axbrowser_navigatorError);
}

public void axbrowser_navigatorError(object pDIsp, ref object URL, ref object frame, ref object statusCode, ref bool Cancel)
{
    statuscodeLabel.Text = statusCode.ToString();
    int.TryParse(statusCode.ToString(), out httpCode);
}
Max
  • 12,622
  • 16
  • 73
  • 101
Jack
  • 16,276
  • 55
  • 159
  • 284
  • 1
    NavigateComplete2 fires when there is no error. So use it to reset your status code. – Hans Passant Jul 13 '14 at 20:15
  • Using this I can just assume no error occured but can't get http status code, since its arguments are `object pDisp, ref object url` where `pDsip` is a `WebBrowser` instance (where we can't get http status code without my `axbrowser_navigatorError()`) and `url` is the url we're handling, right? – Jack Jul 13 '14 at 20:28
  • It looks like just to be the WebBrowser's DocumentCompleted equivalent... – Jack Jul 13 '14 at 20:47

2 Answers2

1

WebBrowser's Refresh is quite different from the Navigate/Navigate2. There is no NavigateComplete2 fired for Refresh. I don't think you can get the status code for Refresh unless you resort to some down-level APP handler hooks. Related: Wpf WebBrowser Refresh.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
0

You can obtain the WebResponse from the NavigationEventArgs on LoadComplete() this should also trigger when the page is refreshed.

Ross Bush
  • 14,648
  • 2
  • 32
  • 55
  • Is this a method from which class? I can't find it in neither `WebBrowser` or `(SHDocVw.WebBrowser)webBrowser1.ActiveXInstance`. Is this msdn.microsoft.com/en-us/library/system.web.ui.page.loadcomplete(v=vs.110).aspx the one you're talking about? – Jack Jul 13 '14 at 19:57
  • Can you use the WebBrowser control from the Toolbox. This one wraps the ActiveX but adds more control. – Ross Bush Jul 13 '14 at 20:02
  • That's what I'm using. Note the cast in the `doSomething()` function. The point you can't get http status code only using a WebBrowser. – Jack Jul 13 '14 at 20:11