K2HttpRequest: asynchronous HTTP request using IWebBrowser2

In my pre­vi­ous battle against Inter­net Explorer add-​on (BHO), I dis­cov­ered some valu­able (and scarce) resources to my aid. Pete‘s TinyBHO and CPete­HttpRe­quest prob­a­bly saved the most of my day.

Then I real­ized a prob­lem: for the browser add-​on that I was writ­ing, I would have to fetch a Web page from a server that requires user login. Under typ­i­cal usage sce­nar­ios, the users are assumed to have logged in the server in a pre­vi­ous ses­sion, so the Web server will autho­rize the user from cook­ies. How­ever, using WinHttp on which CPeteHttpRequest is based on, there seems to be no (simple) ways to obtain the user’s cook­ies and embed it in the HTTP request. I couldn’t find another piece that is as sim­plis­tic as CPeteHttpRequest and also able to deal with my prob­lem at hand.

So I wrote one myself.

K2HttpRequest has the usage of:

K2HttpRequest request;

// Synchronous wstring innerHTML = request.request ( L"http://www.google.com" );

// Asynchronous void foo ( wstring innerHTML ){} request.SetOnComplete ( foo ); request.request ( L"http://www.google.com", true ); K2HttpRequest uses IWebBrowser2 inter­nally, which means it opens a new Inter­net Explorer instance under the hood and loads the Web page with it. Thus, the server will receive login cook­ies as if the user vis­ited the Web page. It will fetch the Web page at the spec­i­fied URL and return the HTML in the <body> ele­ment (I actu­ally intended return the whole <html> ele­ment but I can’t find an easy way to do it, as IHTMLDocument2 doesn’t have get_innerHTML())

This is still a very pre­lim­i­nary ver­sion, and will prob­a­bly improve as my COM/ATL skills improve some day.

Down­load K2HttpRequest(source code ~5KB, Visual C++ 2008)

(Update: The asyn­chro­nous part of this doesn’t work very well. I didn’t know COM enough to use Mar­shalling when I wrote this ver­sion. I’ll prob­a­bly update this some day. If you want to get the asyn­chrounous part work­ing, you’ll prob­a­bly have to add in mar­shalling your­self :p)

30 Responses to “K2HttpRequest: asynchronous HTTP request using IWebBrowser2”

Leave a Reply