Click here to Skip to main content
11,922,416 members (31,362 online)
Click here to Skip to main content
Add your own
alternative version


88 bookmarked


, 3 May 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Synchronized/asynchronized WWW client: HTTP/HTTPS GET, POST, POST multiparts/form-data supported. FTP GET FILE, PUT FILE supported too.


This class is used for HTTP/HTTPS request, and FTP request.

Supported methods are:

    1. GET
    2. POST
    3. POST multiparts/form-data
  • FTP
    1. GET FILE
    2. PUT FILE

Class Overview

// synchronized www client
 class W3Client {
  enum w3t { w3ftp, w3http, w3https };
  enum w3m { reqGet, reqPost, reqPostMultipartsFormdata };
  W3Client(){ _hOpen=NULL; _hConnection=NULL, _hRequest=NULL; }
  virtual ~W3Client(){ InitializePostArguments(); InitializeCookies();}
  // connection handling
  bool Connect(const char *szaddress,
         const char *szuser=NULL, const char *szpassword=NULL, 
      const char *szagent=__W3_DEFAULT_AGENT);
  virtual bool Connect(const char *szaddress, long nport,
           const char *szuser=NULL, const char *szpassword=NULL,
           w3t t=w3http, const char *szagent=__W3_DEFAULT_AGENT);
  const char *GetURI(){ return _szuri.c_str(); }
  void Close();

  // post argument handling
  void InitializePostArguments();
  void AddPostArgument(const char *szname, const int nvalue);
  void AddPostArgument(const char *szname, const long nvalue);
  void AddPostArgument(const char *szname, const float nvalue);
  void AddPostArgument(const char *szname, const double nvalue);
  void AddPostArgument(const char *szname, 
                      const char *szvalue, bool bfile=false);
  // cookie handling
  void InitializeCookies();
  void AddCookie(const char *szname, const double value);
  void AddCookie(const char *szname, const float value);
  void AddCookie(const char *szname, const long value);
  void AddCookie(const char *szname, const int value);  
  void AddCookie(const char *szname, const char *szvalue);
  // http/https request handling
  bool Request(const char *szuri, w3m m=reqGet, const char *szref=NULL);
  unsigned long Response(unsigned char *buf, unsigned long len);
  unsigned int QueryResult();
  const char * QueryContentType();
  unsigned long QueryContentLength();
  unsigned long QueryCookie(unsigned char *buf, 
          unsigned long len, unsigned long idx=0);
  unsigned long QueryRawHeader(unsigned char *buf, unsigned long len);
  // ftp handling
  bool PutFile(const char *szuri, const char *szfile, bool ascii=false);
  bool GetFile(const char *szuri, const char *szfile, bool ascii=false);
  unsigned long PutFile(const char *szuri, unsigned char *buf, 
                        unsigned long len, bool ascii=false);
  unsigned long GetFile(const char *szuri, unsigned char *buf, 
                        unsigned long len, bool ascii=false);


 // Asynchronized www client
 class AsyncW3Client : public W3Client {
  bool Connect(const char *szaddress,
            const char *szuser=NULL,
            const char *szpassword=NULL,
            const char *szagent=__W3_DEFAULT_AGENT);
  bool Connect(const char *szaddress, long nport,
          const char *szuser=NULL, const char *szpassword=NULL,
          w3t t=w3http, const char *szagent=__W3_DEFAULT_AGENT);
  bool Request(const char *szuri, w3m m=reqGet, const char *szref=NULL){
       _hCompleteRequestEvent=::CreateEvent(NULL, FALSE, FALSE, NULL);
       return W3Client::Request(szuri, m, szref);
  unsigned long Response(unsigned char *buf, unsigned long len){
   return W3Client::Response(buf, len);
  void SetCompleteRequest();
  bool WaitCompleteRequest(unsigned long ntime=INFINITE);
  1. Synchronized W3Client

    • Connect(...) method connects to HTTP server.
    • Close() method closes connection. These are used with RequestOfURI(...).
    • InitializePostArguments() method initializes POST arguments.
    • AddPostArgument(...) method is supported so that you can add new POST arguments of the following types: string, int, long, float, double, file.
    • Request(...) method is for you to attempt request for HTTP Request (GET, POST, POST-MULTIPARTFORMDATA) with URL. HTTP METHOD indirector has 3 types.
    • InitializeCookies() method initializes cookie values.
    • AddCookie(...) method adds cookie vars.
      • W3Client::reqGet is HTTP GET Request.
      • W3Cient::reqPost is HTTP POST Request.
      • W3Client::reqPostMultiPartsFormData is HTTP POST Request with BINARY FORM DATA.
    • Response(...) method is that you have HTTP Response by bytes.
    • QueryResult() method is you have receive HTTP Request result value.
  2. Asynchronized W3Client

    • SetCompleteRequest() method commits complete Request to AsyncW3Client.
    • WaitCompleteRequest() method waits for Request to be completed.


  1. Synchronized HTTP GET

    #include <iostream>
    #include "w3c.h"
    using namespace std;
    int main(int argc, char *argv[]){
      W3Client w3;
        char buf[1024]="\0";
        while(w3.Response(reinterpret_cast<unsigned char *>(buf), 1024))
         cout << buf ;
      return 0;
  2. Synchronized HTTP POST multiparts/form-data

    int main(int argc, char *argv[]){
     W3Client client;
      client.AddPostArgument("f[]", "d:\\log1.txt", true);
      client.AddPostArgument("f[]", "d:\\log2.txt", true);
      client.AddPostArgument("f[]", "d:\\log3.txt", true);
      if(client.Request("/test.php", W3Client::reqPostMultipartsFormdata)){
       char buf[1024]="\0";
       while(client.Response(reinterpret_cast<unsigned char*>(buf), 1024)>0){
        cout << buf << endl;
        memset(buf, 0x00, 1024);
     return 0;
  3. Asynchronized HTTP client

    #include <iostream>
    #include <net/w3c.h>
    #include <wt.h>
    #include <windows.h>
    using namespace std;
    class AsDown : public AsyncW3Client, public IWORKERTHREAD {
     AsDown(unsigned int idx):AsyncW3Client(), IWORKERTHREAD(idx){}
     virtual ~AsDown(){}
     virtual void OnWork(){
       unsigned char buf[1024]="\0";
       while(Response(buf, 1024)){
         cout << reinterpret_cast<char*>(buf);
         memset(buf, 0x00, 1024);
    void CALLBACK __getstatus(  HINTERNET hInternet,
                   DWORD_PTR dwContext,
                   DWORD dwInternetStatus,
                   LPVOID lpvStatusInformation,
                   DWORD dwStatusInformationLength
     AsyncW3Client *pcontext=reinterpret_cast<AsyncW3Client*>(dwContext);
     unsigned long nbytes=0;
      cout << "request sending..." << endl;
       unsigned long *pnsent=(unsigned long*)lpvStatusInformation;
       cout << "bytes sent: " << *pnsent << endl;
       cout << "request sent..." << endl;
       INTERNET_ASYNC_RESULT *pAsyncRes = 
              (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
       cout << "Function call finished" << endl;
       cout << "dwResult: " << pAsyncRes->dwResult << endl;
       cout << "dwError:  " << pAsyncRes->dwError << endl;
        cout << "request complete..." << endl;  
    int main(int argc, char *argv[]){ 
     AsDown client(3);
     if(client.Connect("", __getstatus)){
      client.AddPostArgument("f[]", "d:\\log1.txt", true);
      client.AddPostArgument("f[]", "d:\\log2.txt", true);
      client.AddPostArgument("f[]", "d:\\log3.txt", true);
      client.Request("/test.php", AsDown::reqPostMultipartsFormdata);
      client.AddPostArgument("f", "sss");
      client.Request("/test2.php", AsDown::reqPost);  
     return 0;


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


About the Author

Heo Yongseon
Software Developer
Korea (Republic Of) Korea (Republic Of)
Poke tongue | ;-P

You may also be interested in...

Comments and Discussions

QuestionIf you want to get the webpages zipped, then replace W3Client::RequestGet with the following Pin
Eric Schwerzel26-Jun-14 14:25
memberEric Schwerzel26-Jun-14 14:25 
GeneralWorked fine with getting HTTPS pages Pin
Eric Schwerzel11-Jun-14 20:45
memberEric Schwerzel11-Jun-14 20:45 
GeneralNot able to run the sample!! Pin
gu@z12-Nov-08 16:41
membergu@z12-Nov-08 16:41 
QuestionAbout license Pin
PrasVM16-Oct-08 1:07
memberPrasVM16-Oct-08 1:07 
AnswerRe: About license Pin
Yongseon Heo16-Oct-08 2:25
memberYongseon Heo16-Oct-08 2:25 
GeneralSmall bug with cookies Pin
oren.shnitzer24-Aug-08 1:17
memberoren.shnitzer24-Aug-08 1:17 
GeneralA question about AsyncW3Client Pin
Protocol_yatsu23-Feb-08 7:49
memberProtocol_yatsu23-Feb-08 7:49 
QuestionHow to manage a Timeout? Pin
cact26-Sep-07 1:39
membercact26-Sep-07 1:39 
GeneralYour Post Method Fails!!! Pin
Bill SerGio, The Infomercial King29-Jan-07 12:31
memberBill SerGio, The Infomercial King29-Jan-07 12:31 
GeneralRe: Your Post Method Fails!!! Pin
Steve Thresher30-Apr-12 5:55
memberSteve Thresher30-Apr-12 5:55 
Questionquestion about std:string? Pin
typg30-Aug-06 18:08
membertypg30-Aug-06 18:08 
GeneralPOST via SSL does not work for me Pin
koehler12-Jul-06 13:32
memberkoehler12-Jul-06 13:32 
Generalif(_hOpen || _hOpen!=INVALID_HANDLE_VALUE) Pin
cp66331111-Jul-06 0:40
membercp66331111-Jul-06 0:40 
GeneralBug with ::HttpOpenRequest in your class [modified] Pin
sbytov29-Jun-06 0:50
membersbytov29-Jun-06 0:50 

First at all thank you for this classes, i like to use it Smile | :)

But it looks like it is a bug here. I founded it, when i
install IE 7 beta (with new version of wininet.dll in there).
In methods RequestGet, RequestPost and RequestPost2
you use ::HttpOpenRequest with wrong param szAcceptType:

_hRequest=::HttpOpenRequest( _hConnection,
szuri, // Object Name
HTTP_VERSION, // Version
szref, // Reference
&szAcceptType, // Accept Type
NULL); // context call-back point.

But "Accept Type"-parameter must be (from MSDN)

[in] Long pointer to a null-terminated (!!!) array of string pointers indicating content types accepted by the client. If this parameter is NULL, no types are accepted by the client. Servers interpret a lack of accept types to indicate that the client accepts only documents of type "text/*" (that is, only text documents, and not pictures or other binary files).

So, i think - right will be something, like this:

_hRequest=::HttpOpenRequest( _hConnection,
szuri, // Object Name
HTTP_VERSION, // Version
szref, // Reference
szAcceptTypes, // Accept Type
INTERNET_FLAG_NO_CACHE_WRITE | https? INTERNET_FLAG_SECUREBlush | :O ), NULL); // context call-back point.

Sorry for my bad English, i learned German )

Wbr, Kirill Sbytov.

-- modified at 5:56 Thursday 29th June, 2006
GeneralRe: Bug with ::HttpOpenRequest in your class Pin
Goatboy16028-Aug-06 11:11
memberGoatboy16028-Aug-06 11:11 
GeneralRe: Bug with ::HttpOpenRequest in your class Pin
praveen7921-Nov-08 0:35
memberpraveen7921-Nov-08 0:35 
Generalhere,how to login out yahoo email Pin
shellhy26-Jun-06 1:23
membershellhy26-Jun-06 1:23 
QuestionIWORKERTHREAD [modified] Pin
daman37120-Jun-06 8:37
memberdaman37120-Jun-06 8:37 
daman37122-Jun-06 9:31
memberdaman37122-Jun-06 9:31 
General222 Pin
shellhy13-Jun-06 2:07
membershellhy13-Jun-06 2:07 
QuestionDialog and String Table all in KOREAN, How do I convert to English ? Pin
cfilorux3-May-06 13:46
membercfilorux3-May-06 13:46 
GeneralSessions Pin
eusto19-Apr-06 3:15
membereusto19-Apr-06 3:15 
GeneralRe: Sessions Pin
Yongseon Heo19-Apr-06 16:31
memberYongseon Heo19-Apr-06 16:31 
GeneralDownload file Pin
meouvn9-Mar-06 17:04
membermeouvn9-Mar-06 17:04 
GeneralRe: Download file Pin
_aleksei31-Dec-06 4:13
member_aleksei31-Dec-06 4:13 
QuestionPost xml data Pin
tino5516-Feb-06 23:50
membertino5516-Feb-06 23:50 
QuestionSmall bug? or my mistake? Pin
Yails5-Dec-05 6:05
memberYails5-Dec-05 6:05 
AnswerRe: Small bug? or my mistake? Pin
Yongseon Heo19-Dec-05 18:09
memberYongseon Heo19-Dec-05 18:09 
Generalurl encoding Pin
gkutiel22-Oct-05 8:43
membergkutiel22-Oct-05 8:43 
GeneralRe: url encoding Pin
Yongseon Heo19-Dec-05 18:08
memberYongseon Heo19-Dec-05 18:08 
GeneralProgress in uploading file Pin
Akbaraka10-Aug-05 5:46
memberAkbaraka10-Aug-05 5:46 
GeneralRe: Progress in uploading file Pin
Yongseon Heo10-Aug-05 17:09
memberYongseon Heo10-Aug-05 17:09 
GeneralRe: Progress in uploading file Pin
marlongrech30-Oct-06 6:53
membermarlongrech30-Oct-06 6:53 
GeneralRe: Progress in uploading file Pin
marlongrech31-Oct-06 1:06
membermarlongrech31-Oct-06 1:06 
QuestionRe: Progress in uploading file Pin
Angel Kafazov7-Jan-09 5:42
memberAngel Kafazov7-Jan-09 5:42 
GeneralCrash / Fix when the connection internet is offline and bug in the return of the function Connect(...). Pin
alanlive30-Jun-05 22:38
memberalanlive30-Jun-05 22:38 
GeneralBug in AsyncW3Client Pin
Dean Hallman24-Jun-05 15:30
memberDean Hallman24-Jun-05 15:30 
GeneralHTTPS Not Working Pin
neverneverland15-Jun-05 7:26
sussneverneverland15-Jun-05 7:26 
GeneralRe: HTTPS Not Working Pin
TomazZ28-Jul-05 4:49
memberTomazZ28-Jul-05 4:49 
GeneralRe: HTTPS Not Working Pin
TomazZ28-Jul-05 4:54
memberTomazZ28-Jul-05 4:54 
GeneralRe: HTTPS Not Working Pin
dungbkhn22-Aug-05 19:16
memberdungbkhn22-Aug-05 19:16 
GeneralRe: HTTPS Not Working Pin
TomazZ5-Dec-05 3:08
memberTomazZ5-Dec-05 3:08 
GeneralRe: HTTPS Not Working Pin
szuzso14-Dec-05 3:40
memberszuzso14-Dec-05 3:40 
GeneralRe: HTTPS Not Working Pin
meouvn8-Mar-06 21:13
membermeouvn8-Mar-06 21:13 
GeneralRe: HTTPS Not Working Pin
meouvn9-Mar-06 17:08
membermeouvn9-Mar-06 17:08 
GeneralRe: HTTPS Not Working Pin
zhirenze9-Dec-06 4:47
memberzhirenze9-Dec-06 4:47 
GeneralUnicode Version Pin
SnowshoeMJ20-May-05 10:42
memberSnowshoeMJ20-May-05 10:42 
GeneralRe: Unicode Version Pin
Yongseon Heo24-May-05 15:09
memberYongseon Heo24-May-05 15:09 
GeneralRe: Unicode Version [modified] Pin
HongJin Kim13-Jun-06 17:52
memberHongJin Kim13-Jun-06 17:52 
GeneralCan't complie on VS.NET 2003 Pin
moshem10-May-05 7:55
membermoshem10-May-05 7:55 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151120.1 | Last Updated 4 May 2004
Article Copyright 2004 by Heo Yongseon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid