Http Monitor for Webbrowser Control
Introduction
There are various tools available to monitor Http traffic that is being sent and received from different processes. Fiddler is one such good example. All these programs open a port and filter Http traffic based on process id. But if an C# app consists of multiple browsers they fail to tell which request was sent by which browser.The C# browser control only provides Navigating and Navigated events and does not give any idea about the requests that it sends (e.g. loading of images etc.).
This article provides an ATL COM Dll that can monitor Http traffic from individual browsers.
Background
While working on a project which required the same, I stumbled upon PassThruApp by Igor Tandetnik.
csExWBDLMan.dll ("csExWBDLMan COM library" from The most complete C# Webbrowser wrapper control) is one implementation of PassThru App that provides the requests but does not provide information of redirections and data received in the requests. So, I decided to write a custom code just for monitoring Http traffic based on both PassThru App and csExWBDLMan.dll.
About PassThru App
"It is an object that implements both sides of URL moniker-to-APP communication, that is, it implements both IInternetProtocol and IInternetProtocolSink / IInternetBindInfo. We register it as a temporary handler for a standard protocol, such as HTTP. Now whenever an HTTP request needs to be sent, URL moniker will create an instance of our pAPP and ask it to do the job. The pAPP then creates an instance of a standard APP for the protocol in question (I call it a target APP, or tAPP...) and acts as its client. At this point, our pAPP becomes a proverbial man-in-the-middle. In the simplest case, any method call made by URL Moniker on pAPP is forwarded to tAPP, and any method call made by tAPP on pAPP is forwarded back to URL Moniker. The pAPP gets to observe, and if desired modify, every bit of information relevant to this request passing back and forth between the moniker and the tAPP. QED" - Igor Tandetnik
The Code
The code extends classes provided by PassThru App. There are two main classes
- MonitorSink - MonitorSink extends PassthroughAPP::CInternetProtocolSinkWithSP that implements IInternetProtocolSink
- CTestAPP - CTestAPP extends PassthroughAPP::CInternetProtocol that implements IInternetProtocol
class MonitorSink : public PassthroughAPP::CInternetProtocolSinkWithSP<MonitorSink>, public IHttpNegotiate {..
class CTestAPP : public PassthroughAPP::CInternetProtocol<TestStartPolicy> {..
Now we can intercept requests using
- Request - MonitorSink::BeginningTransaction
- Response - MonitorSink::OnResponse
- Redirection - MonitorSink::ReportProgress when ulStatusCode from IInternetProtocolSink->ReportProgress(ulStatusCode.. is BINDSTATUS_REDIRECTING
- Data received - CTestAPP::Read
But the problem is that we are using Asynchronous Pluggable Protocol and all the requests are done asynchronously. So we get all, but cannot say which response belonged to which request. Moreover the data is received asynchronously in chunks.
The best solution is that if we get unique id for a transaction(i.e. unique id attached request, response and data received) then we will be able to weave the async calls back together. Here we get lucky
- IInternetBindInfo for a request is most of the times unique and is available in all the methods. But sometimes it is reused by the Browser.
- The Url to which request is made is also most likely to be unique.
Using the code
When you attach your browser with HttpMonitor.dll, on each request, response etc. an event is fired with all the required arguments. There are four events available
- OnRequest(int id, string url, string headers, string method, object postData)
- OnRedirect(int id, int redirectedId, string url, string redirectedUrl, string responseHeaders, string requestHeaders)
- OnDataRecieved(int id, string url, object data, int status)
- OnResponse(int id, string url, int responseCode, string headers)
if (monitor == null) { monitor = new HttpMonitorLib.HttpMonClass(); monitor.IEWindow = GetTopWindow(GetTopWindow(GetTopWindow(webBrowser1.Handle))).ToInt32(); monitor.OnRequest += new HttpMonitorLib._IHttpMonEvents_OnRequestEventHandler(monitor_OnRequest); . .
For example, the following function will be executed whenever the browser sends a request.
private void monitor_OnRequest(int id, string url, string headers, string method, object postData) { //code here }
The id specifies unique id associated with that particular Http transaction.
发表评论
SpSb9b Really informative article post.Thanks Again. Keep writing.
j02gDm Xanax (Alprazolam) is used to treat anxiety disorders and panic attacks. Alprazolam is in a class of
Way cool! Some extremely valid points! I appreciate you writing this article and the rest of the site is also really good.
FIxzMg I simply want to say I am new to blogging and actually savored your web site. More than likely I am going to bookmark your site. You surely have wonderful writings. Regards for revealing your blog.
O6oss9 I think other website proprietors should take this website as an model, very clean and great user genial style and design, let alone the content. You are an expert in this topic!
cwz55H It as enormous that you are getting thoughts from this post
DI9bGP There is noticeably a bundle to know concerning this. I presume you completed positive kind points in facial appearance also.
hVTe35 Name (???????????). Mail (will not be published) (???????????). Website...
egJbOC Then you all know which is right for you.
mMjrDw If you are concerned to learn Web optimization techniques then you should read this article, I am sure you will obtain much more from this article concerning SEO.
55Xjfd Thanks for sharing, this is a fantastic blog post.Really looking forward to read more. Want more.
fOm2Vp I think this is a real great article. Fantastic.
nQ6skI Major thanks for the article.Really looking forward to read more.
MBMA3c You made some decent points there. I looked on the internet for the issue and found most persons will go along with with your site.
HHNYbO Really informative blog article.Really thank you! Great.
7wFgKW website who has shared this enormous piece of writing at
BPbC0S Wow! Thank you! I constantly needed to write on my website something like that. Can I implement a fragment of your post to my blog?
GHJEB5 You have remarked very interesting points ! ps decent website.
vtwj3G Vilma claimed that the cheap jersey problems of hackers to emails.
OqHAAn Thank you ever so for you article post.Much thanks again. Really Great.
HemwzP There is visibly a lot to know about this. I feel you made various good points in features also.
winaEx Your mode of telling the whole thing in this article is in fact good, all be capable of without difficulty understand it, Thanks a lot.
w9VwxH You are my inhalation , I possess few blogs and occasionally run out from to post.
YQg7Pd Is that this a paid subject or did you customize it your self?
l5mSHa which gives these kinds of stuff in quality?
qYDeLL Very neat article.Really looking forward to read more. Keep writing.
gDIPdf very couple of internet sites that come about to become comprehensive beneath, from our point of view are undoubtedly very well really worth checking out
Y6gBYm important site Of course, you are not using some Under-developed place, The united kingdom possesses high water-purification benchmarks
cHlCMy There are so many options out there that I am completely confused.. Any recommendations? Thanks!
I will right away grab your rss as I can at to find your email subscription hyperlink or newsletter service. Do you have any? Please allow me realize so that I may subscribe. Thanks.
X0sXWS Major thankies for the article. Awesome.
EfdKh7 It'аs in reality a nice and useful piece of info. I am glad that you just shared this useful info with us. Please stay us up to date like this. Thank you for sharing.
awL9ft There as certainly a great deal to know about this issue. I really like all the points you have made.
1Zi5KN It as nearly impossible to find well-informed people for this subject, however, you sound like you know what you are talking about! Thanks
wMdRQW
l6jyZd
BQcnjW Thanks for the article post.Really looking forward to read more. Keep writing.
JuAzZ2 Wohh precisely what I was looking for, appreciate it for posting.
Tz15u1 You can definitely see your enthusiasm in the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. Always go after your heart.
BhumNU Thank you ever so for you blog post.Thanks Again.
lpeoCt I truly appreciate this article.Much thanks again. Much obliged.
DGlCPh Really enjoyed this blog. Awesome.
sWvtpN It as hard to come by well-informed people in this particular subject, however, you sound like you know what you are talking about! Thanks
UggRsJ I cannot thank you enough for the blog post.Much thanks again. Want more.
OKLljh I'm not sure why but this web site is loading incredibly slow for me. Is anyone else having this issue or is it a issue on my end? I'll check back later on and see if the problem still exists.
B1lsmh I've learn some excellent stuff here. Definitely worth bookmarking for revisiting. I wonder how so much attempt you put to make any such wonderful informative web site.
7qilVG Thanks a lot for sharing this with all folks you actually recognize what you are talking about! Bookmarked. Please also consult with my site =). We could have a hyperlink exchange contract between us!
superb captures!
fUv6To wonderful put up, very informative. I'm wondering why the other specialists of this sector do not notice this. You should continue your writing. I'm sure, you've a great readers' base already!
ZM33dL I will immediately grasp your rss feed as I can not in finding your e-mail subscription link or newsletter service. Do you have any? Kindly let me recognise so that I could subscribe. Thanks.