Click here to Skip to main content
15,171,729 members
Articles / Mobile Apps / Windows Mobile
Posted 17 Nov 2009


172 bookmarked

An Asynchronous HTTP Request WinINet Wrapper in C++

Rate me:
Please Sign up or sign in to vote.
4.90/5 (45 votes)
25 Aug 2011CPOL2 min read
An asynchronous HTTP download class for C++
Image 1


WinINet makes network programming easier, but it's difficult to use for C++ because of its C-style interface, so I write this code to wrap it. Using it, you can easily create an asynchronous HTTP request and receive event callback. It can be applied to MFC and ATL projects.

This code mainly consists of two classes: class FCHttpRequest and class FCHttpRequestManager.

Class FCHttpRequest is responsible for implementing send HTTP request and receive response, it's a wrapper class for WinINet.

Class FCHttpRequestManager is a management of FCHttpRequest, responsible for add, delete FCHttpRequest object and receive event from FCHttpRequest.

Similar to the STL and Boost, all source code consists of .h and .inl file, you can easily integrate it into your program.

How to Use

  1. Include http_request_manager.h file in your project. normally include it at the end of the stdafx.h.
  2. Create HTTP request manager class derived from class FCHttpRequestManager.
    class CMyManager : public FCHttpRequestManager
    // multiple inheritance attach an exist class
    class CMyManager : public CDialog,
                       public FCHttpRequestManager
        virtual void OnAfterRequestSend (FCHttpRequest& rTask)
            // ...
        virtual void OnAfterRequestFinish (FCHttpRequest& rTask)
            // ...

    The manager will receive two events: OnAfterRequestSend and OnAfterRequestFinish, overwrite event you find interesting. Most of the time, we should overwrite OnAfterRequestFinish to process received data.

  3. Now, you can send HTTP request by calling AddRequest.


... Download File

Send HTTP request:

// download whole file
AddDownload (_T("")) ;

// download file and specifies the starting position
h.m_url = _T("") ;
h.m_start = 30 ; // in byte
AddRequest (h) ;

Response finish event, process downloaded data:

void OnAfterRequestFinish (FCHttpRequest& rTask)
    const HTTP_RESPONSE_INFO   & r = rTask.GetResponseInfo() ;

    bool   bOK = false ;
    if (r.m_status_code == HTTP_STATUS_OK)
        if (r.m_content_length)
            // avoid network broken during downloading
            if (r.m_content_length == rTask.GetTotalReceiveByte())
                bOK = true ;
            // no Content-Type field, maybe is a dynamic page, such as PHP, ASP...
            if (r.m_final_read_result)
                bOK = true ;

    if (bOK)
        std::string   receive_data ;
        rTask.PopReceived (receive_data) ;

        // ... process received data

... Specifying the User-Agent

// default User-Agent is same to User-Agent of IE
h.m_url = _T("") ;
h.m_user_agent = _T("My User-Agent") ;
AddRequest (h) ;

... Custom HTTP Header

h.m_url = _T("") ;

// \r\n at end of each line
h.m_header += _T("Referer:\r\n") ;
h.m_header += _T("Accept: */*\r\n") ;
h.m_header += _T("x-flash-version: 10,0,0,1\r\n") ;

AddRequest (h) ;

... Specifying the Proxy

h.m_url = _T("") ;
h.m_proxy_ip = _T("") ;
h.m_proxy_port = 8080 ;
AddRequest (h) ;

... Decompress GZip HTTP Data

This feature requires the ZLib library, you can get it from
Include headers in the following order:

#include "zlib.h"
#include "http_request_manager.h"
#include "utility/gzip_decompress.h"

Add Accept-Encoding field in request header, notify the server that you can accept gzip compressed data:

h.m_url = _T("") ;
h.m_header += L"Accept-Encoding: gzip, deflate\r\n" ;
AddRequest (h) ;

In finish callback, you can use gzip_decompress to decompress received data.

void OnAfterRequestFinish (FCHttpRequest& rTask)
    std::string   receive_data ;
    rTask.PopReceived (receive_data) ;

    if (IsReceiveGZipStream(rTask) && receive_data.size())
        std::string   raw_data ;
        if (gzip_decompress (receive_data.c_str(), receive_data.size(), raw_data))
            receive_data = raw_data ;

... Post multipart/form-data

// this example loads image to memory, then posts to server using multipart/form-data
std::vector<byte />   buf ;
FCFileEx::Read (_T("c:\\1.jpg"), buf) ;

h.m_url = _T("") ;
h.AddMultipartFormData ("param1", "value1") ;
h.AddMultipartFormData ("param2", "value2") ;
h.AddMultipartFormData ("pic", &buf[0], buf.size(), "1.jpg") ;
h.EndMultipartFormData() ;
m_task_id = AddRequest (h) ;


h.m_url = _T("") ;

// ignores errors that can be caused by the certificate 
// host name of the server not matching the host name in the request

// ignores errors that can be caused by an expired server certificate

AddRequest (h) ;


  • 25th August, 2011: V2.0
    • Refactoring code, more features, more efficient
    • Added multipart/form-data POST support
    • Added HTTPS support
    • Added GZip decompress support
  • 30th January, 2010: V1.1
    • Renamed from FCHttpDownloadExec to FCHttpDownload
    • Fixed a bug when popping a modal dialog in callback
    • Modified interface of FCHttpRequestManager
    • Got default User-Agent from Internet Explorer
  • 17th November, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader PhoXo
China China
graduate from University of Science and Technology of China at 2002.

Now I work at

Comments and Discussions

QuestionNot multiplatform Pin
Jaime Stuardo - Chile12-Apr-17 9:11
MemberJaime Stuardo - Chile12-Apr-17 9:11 
QuestionVery Good Pin
DeanV10-Oct-16 8:40
MemberDeanV10-Oct-16 8:40 
GeneralMy vote of 5 Pin
MaitreyaBuddha16-May-15 1:58
MemberMaitreyaBuddha16-May-15 1:58 
GeneralAwesome Asynch and event-driven HTTP class! Pin
MaitreyaBuddha16-May-15 1:57
MemberMaitreyaBuddha16-May-15 1:57 
QuestionReally nice Pin
Jonathan Potter11-Mar-15 21:32
MemberJonathan Potter11-Mar-15 21:32 
QuestionSetProcessWorkingSetSize Pin
anandssunku6-Jul-14 21:04
Memberanandssunku6-Jul-14 21:04 
QuestionSource code Pin
Michael Haephrati20-Feb-13 8:49
mvaMichael Haephrati20-Feb-13 8:49 
GeneralMy vote of 5 Pin
Michael Haephrati20-Feb-13 8:45
mvaMichael Haephrati20-Feb-13 8:45 
QuestionAfter update... how make this synchronous?? Pin
mrgalwen30-Aug-12 7:37
Membermrgalwen30-Aug-12 7:37 
AnswerRe: After update... how make this synchronous?? Pin
crazybit4-Sep-12 22:26
Membercrazybit4-Sep-12 22:26 
Generalvery nice! Pin
liuchuan29014-Jun-12 23:18
Memberliuchuan29014-Jun-12 23:18 
GeneralMy vote of 5 Pin
ZzwKathleen4-Jun-12 17:03
MemberZzwKathleen4-Jun-12 17:03 
Question居然又碰到了,呵呵 Pin
shengcheng_jin30-Aug-11 0:50
Membershengcheng_jin30-Aug-11 0:50 
AnswerRe: 居然又碰到了,呵呵 Pin
crazybit30-Aug-11 1:23
Membercrazybit30-Aug-11 1:23 
GeneralLast example not working Pin
KL@DotNet4-Jan-10 2:37
MemberKL@DotNet4-Jan-10 2:37 
GeneralRe: Last example not working Pin
KL@DotNet4-Jan-10 5:32
MemberKL@DotNet4-Jan-10 5:32 
GeneralRe: Last example not working Pin
crazybit4-Jan-10 18:58
Membercrazybit4-Jan-10 18:58 
QuestionMake the code synchronous ? Pin
darthmaul30-Dec-09 8:53
Memberdarthmaul30-Dec-09 8:53 
AnswerRe: Make the code synchronous ? Pin
crazybit4-Jan-10 18:50
Membercrazybit4-Jan-10 18:50 
GeneralRe: Make the code synchronous ? Pin
darthmaul4-Jan-10 23:32
Memberdarthmaul4-Jan-10 23:32 
QuestionSupport for https ? Pin
irol20-Nov-09 0:54
professionalirol20-Nov-09 0:54 
AnswerRe: Support for https ? Pin
crazybit23-Nov-09 3:55
Membercrazybit23-Nov-09 3:55 
QuestionSupport pause and resume functions? Pin
Symfund18-Nov-09 15:13
MemberSymfund18-Nov-09 15:13 
Are there pause, resume functions?
AnswerRe: Support pause and resume functions? Pin
crazybit18-Nov-09 16:07
Membercrazybit18-Nov-09 16:07 
AnswerRe: Support pause and resume functions? Pin
Anton Afanasyev30-Jan-10 15:33
MemberAnton Afanasyev30-Jan-10 15:33 

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.