views:

151

answers:

2

Is there way to capture and print out all of the requests and responses from the web browser control including asynchronous requests?

+3  A: 

Have you thought about using Fiddler? They even have an API that you might be able to use for this.

Edit

You can try using the Navigated event or LoadCompleted event for getting response information. You might be able to use the Navigating event for request information. These events might occur with async requests, but I'm not sure on that.

The Edit Strikes Back

Well I did some testing using the above events, and I'm afraid I got nothing. The events were not giving me anything that I could use. Even though they had access to webresponse and webrequest, they were always nothing/null in my testing.

I guess the sad reality is that the web browser control was not intended to be used the way you want. It's just for serving web pages/data and does not provide you the access to look into the data. I think they did it as a C.Y.A. measure to prevent someone from making a malware/spyware browser.

Your best bet is going to be coupling the web browser with fiddler (or some other 'sniffing' library).

Tony Abrams
I was kind of hoping that there were some event I could capture in the web browser control.
Kenneth J
regarding 'The Edit Strikes Back' result, I was about to comment saying the same thing. What do you think about using the WebClient class to make the requests then using the browser control to display the results. I haven't had much time to experiement but I think that will be my next step.
Kenneth J
That is doable, using the NavigateToStream (on webbrowser) with OpenRead (on webclient), but I am not sure on which events to use. Sounds like something fun to test out.
Tony Abrams
+1  A: 

Another way to accomplish this is by implementing your own Asynchronous Pluggable Protocol handler, but it's not a trivial task. Basically, APPs are the mechnanism used by Internet Explorer to download data from a url, via URLMON. You can implement you own APP that replaces the default handler that URLMON uses, which would allow you control over all requests and responses.

Instead of replacing the default handler you could also implement an APP, which wraps it. A commonly referred example is the PassThroughAPP created by Igor Tandetnik, which shows how something like this could be done. You can also find a C# implementation in this code project article. I believe the interface you would be most interested in is IHttpNegotiate, and the OnResponse method, which allows a client the examine the request and response headers.

There are some issues with the PassThroughApp, which are detailed, along with a possible solution here. A few more resources are included below.

http://support.microsoft.com/?id=kb;en-us;303740 http://www.codeproject.com/KB/aspnet/AspxProtocol.aspx

Garett