Capturing all requests and responses from the web

2020-06-04 02:39发布

问题:

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

回答1:

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).



回答2:

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