Click here to Skip to main content
13,556,624 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


48 bookmarked
Posted 31 Jul 2008
Licenced LGPL3

A Simple Windows HTTP Wrapper Using C++

, 31 Jul 2008
Rate this:
Please Sign up or sign in to vote.
Gets the content of a web page into a project without the WinHttp APIs mixed in my source code using a WinHttp wrapper.


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 is as follows.

WinHttpClient client(L"");
wstring httpResponseHeader = client.GetHttpResponseHeader();
wstring httpResponse = client.GetHttpResponse();


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.


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


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Software Developer Philips
China China
Cheng Shi is a software developer in China. He is interested in COM, ATL, Direct3D, etc. He is now working for Philips.

Cheng Shi loves Formula1 and watchs every Grand Prix. He is dreaming to be a racing car driver. Hope his dream can come true.

You may also be interested in...


Comments and Discussions

Questionitd doesn't works with windows xp somehow Pin
sandeepcyber28-Oct-17 1:12
membersandeepcyber28-Oct-17 1:12 
QuestionAbout put , delete Pin
liaoyuandeyehuo28-Aug-13 0:06
memberliaoyuandeyehuo28-Aug-13 0:06 
Generalthanks Pin
qhgongzi23-Apr-11 23:17
memberqhgongzi23-Apr-11 23:17 
GeneralWrong order in the command-line arguments Pin
jcdmelo13-Jul-10 17:16
memberjcdmelo13-Jul-10 17:16 
GeneralRe: Wrong order in the command-line arguments Pin
shicheng13-Jul-10 19:05
membershicheng13-Jul-10 19:05 
GeneralRe: Wrong order in the command-line arguments Pin
jcdmelo14-Jul-10 3:12
memberjcdmelo14-Jul-10 3:12 
GeneralRe: Wrong order in the command-line arguments Pin
shicheng14-Jul-10 4:25
membershicheng14-Jul-10 4:25 
GeneralFollowing the KISS principle (Was "Re: Wrong order in the command-line arguments") Pin
jcdmelo14-Jul-10 5:45
memberjcdmelo14-Jul-10 5:45 
GeneralThe udpated version of WinHttpClient Pin
shicheng20-Mar-10 18:17
membershicheng20-Mar-10 18:17 
GeneralDon't expose local symbols Pin
bling17-Mar-10 6:50
memberbling17-Mar-10 6:50 
GeneralRe: Don't expose local symbols Pin
shicheng17-Mar-10 16:54
membershicheng17-Mar-10 16:54 
QuestionCan I use this under the LGPL V3 Pin
Colin Critch15-Feb-10 2:49
memberColin Critch15-Feb-10 2:49 
AnswerRe: Can I use this under the LGPL V3 Pin
shicheng17-Mar-10 16:51
membershicheng17-Mar-10 16:51 
GeneralRe: Can I use this under the LGPL V3 Pin
Colin Critch26-Mar-10 2:00
memberColin Critch26-Mar-10 2:00 
GeneralRe: Can I use this under the LGPL V3 Pin
shicheng27-Mar-10 1:22
membershicheng27-Mar-10 1:22 
GeneralMy vote of 2 Pin
.Shoaib8-Dec-09 22:00
member.Shoaib8-Dec-09 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180515.1 | Last Updated 31 Jul 2008
Article Copyright 2008 by shicheng
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid