Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Recently I needed to get the content of a web page into a project and I didn't want to have WinHttp APIs mixed in my source code so I created a simple WinHttp wrapper named WinHttpClient.

WinHttpClient takes a URL as a parameter, sends the HTTP request and gets the response. The method that sends the HTTP request is a synchronous method so it should NOT be called in the UI thread. WinHttpClient is thread safe which means it can be used in many threads at one time.

Using the Code

Using WinHttpClient is pretty simple. The class diagram is as follows.

For example, the code snap to get the content of http://www.codeproject.com/ is as follows.

    WinHttpClient client(L"http://www.codeproject.com/");
    client.SendHttpRequest();
    wstring httpResponseHeader = client.GetHttpResponseHeader();
    wstring httpResponse = client.GetHttpResponse();

Additional

There are several things that may interest you.

  1. The method SendHttpRequest is a synchronous method and it will retry several times until it succeeds which causes this method to take a long time to finish. So do not use this in a UI thread. It is recommended to create some worker threads and use WinHttpClient in them since WinHttpClient is a thread safe class.
  2. You can parse and enumerate the URLs in the string returned by the method GetHttpResponse and get the contents of the them and do it over and over again. Then you will get the contents of a whole website.
  3. URLs with default index pages can be handled well. Fox example:
  4. I don’t really know how the HTTP verb affects the result. So a user can select use the GET or POST verb to send the HTTP request through the parameter of SendHttpRequest. The default verb is GET.

Points of Interest

When using MultiByteToWideChar to convert to the wide characters, deleting the buffer I created to hold the output wide leads to a crash if I set the last character to 0. It is strange and I didn't figure it out.

Finally

This is my first time writing a technical article. I hope I explained everything clearly.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralThe udpated version of WinHttpClient
shicheng
19:17 20 Mar '10  
The udpated version of WinHttpClient is available at A Fully Featured Windows HTTP Wrapper in C++[^].
GeneralDon't expose local symbols
bling
7:50 17 Mar '10  
Please don't define stuff (eg. static variables and #define'd symbols) in the header file that will only be used in the implementation file. Client code that includes the header file does not need redundant copies of the static variables and #define'd symbols.

I use the old WinInet API for compatibility with older versions of Windows - but your sample code makes a nice wrapper for the new HTTP API.
GeneralRe: Don't expose local symbols
shicheng
17:54 17 Mar '10  
Hi Bling,

What to do if I cannot define static or const variables in the header files? I don't see a good solution.

And I have updated the code to a much featured Windows HTTP client. You can downloaded from (<a href="http://shicheng.name/wp-content/uploads/2010/03/WinHttpClient_20100318.zip">http://shicheng.name/wp-content/uploads/2010/03/WinHttpClient_20100318.zip</a>[<a href="http://shicheng.name/wp-content/uploads/2010/03/WinHttpClient_20100318.zip" target="_blank" title="New Window">^</a>]). I will write an new artical soon.

Thanks,
Cheng Shi
GeneralCan I use this under the LGPL V3
Colin Critch
3:49 15 Feb '10  
Hi,
Can I use this under the LGPL V3?
Thank you very much for the example.
GeneralRe: Can I use this under the LGPL V3
shicheng
17:51 17 Mar '10  
Hi Colin,

You can use the code whatever you want to. You can use it in commercial project and modify the code without notifying me. And I have updated the code to a much featured Windows HTTP client. You can downloaded from (http://shicheng.name/wp-content/uploads/2010/03/WinHttpClient_20100318.zip[^]).

Have funSmile

Thanks,
Cheng Shi
GeneralMy vote of 2
.Shoaib
23:00 8 Dec '09  
bad design


Last Updated 31 Jul 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010