In my previous battle against Internet Explorer add-on (BHO), I discovered some valuable (and scarce) resources to my aid. Pete‘s TinyBHO and CPeteHttpRequest probably saved the most of my day.
Then I realized a problem: for the browser add-on that I was writing, I would have to fetch a Web page from a server that requires user login. Under typical usage scenarios, the users are assumed to have logged in the server in a previous session, so the Web server will authorize the user from cookies. However, using WinHttp on which CPeteHttpRequest is based on, there seems to be no (simple) ways to obtain the user’s cookies and embed it in the HTTP request. I couldn’t find another piece that is as simplistic as CPeteHttpRequest and also able to deal with my problem at hand.
So I wrote one myself.
K2HttpRequest has the usage of:
1 2 3 4 5 6 7 8 9 | 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 internally, which means it opens a new Internet Explorer instance under the hood and loads the Web page with it. Thus, the server will receive login cookies as if the user visited the Web page. It will fetch the Web page at the specified URL and return the HTML in the <body> element (I actually intended return the whole <html> element but I can’t find an easy way to do it, as IHTMLDocument2 doesn’t have get_innerHTML())
This is still a very preliminary version, and will probably improve as my COM/ATL skills improve some day.
Download K2HttpRequest(source code ~5KB, Visual C++ 2008)
(Update: The asynchronous part of this doesn’t work very well. I didn’t know COM enough to use Marshalling when I wrote this version. I’ll probably update this some day. If you want to get the asynchrounous part working, you’ll probably have to add in marshalling yourself :p)


Thanks, I found this code useful.
…but if I try to use it to download, for example, a file with a .js extension, then it pops up the dialog to the user asking them if they want to open or save, which I decidedly don’t want. Any suggestions for that?
Edward:
I guess you would be looking for implementing a custom download manager.