Click here to Skip to main content
13,902,840 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


26 bookmarked
Posted 10 Dec 2009
Licenced CPOL

Asynchronous WinHTTP Library

, 10 Dec 2009
Rate this:
Please Sign up or sign in to vote.
Tiny WinHTTP API wrapper library for asynchronous HTTP with callback handler


The AsyncWinHttp is a simple wrapper of WinHTTP to achieve asynchronous HTTP. WinHtpp API is a replacement of WinINet API (see About WinHTTP for more information of why Microsoft recommends to use WinHttp API).

The library provides a very simple interface, the user can register a callback function when initializing the class. And the callback will be invoked when HTTP finished. The interface is shown as:


This code is based on the sample of WinHTTP from MSDN, with a wrapper for simple use. 

Using the Code

To use the library is very simple, all you need is the two files:

  • AsyncWinHttp.h
  • AsyncWinHttp.cpp

First, you need to define your callback functions. These functions will be invoked when HTTP finished (whether succeeded or failed):

void WinHttp_CallBack(AsyncWinHttp* asyncWinHttp)
  // check if success or failed
  if (asyncWinHttp->status.Status() == ASYNC_WINHTTP_ERROR)
    // print error message
    printf("%S", asyncWinHttp->status.Desc().c_str());
  else // success
    // print response, do your handling here ...
    std::string response;
    printf("%s", response.c_str());

Now you can invoke the request to HTTP resources:

  AsyncWinHttp http;
  http.SetTimeout(3 * 60 * 1000);  // time out is 3 minutes

Of course, you can use a Windows event to wait for the asynchronous HTTP call to finish, as shown in the demo, but I think most of you will not do so, since we need a really asynchronous HTTP. ;)


  • 11th December, 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

China China
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHello, the code maybe has memory leakage Pin
fluxischarge24-Jun-13 11:27
memberfluxischarge24-Jun-13 11:27 
BugStatus code not getting set on successful SendRequest Pin
Member 978173110-Apr-13 9:12
memberMember 978173110-Apr-13 9:12 
Questionuse with proxy model and response error 12030 Pin
david_jiang9-Apr-13 3:57
memberdavid_jiang9-Apr-13 3:57 
Generalan error :REQUEST_ERROR - error 12002, result API_RECEIVE_RESPONSE Pin
jackinelee23-Apr-10 21:43
memberjackinelee23-Apr-10 21:43 
GeneralRe: an error :REQUEST_ERROR - error 12002, result API_RECEIVE_RESPONSE Pin
zoufeiyy25-Mar-12 2:41
memberzoufeiyy25-Mar-12 2:41 

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 | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 11 Dec 2009
Article Copyright 2009 by zoufeiyy
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid