Click here to Skip to main content
Licence 
First Posted 23 Mar 2003
Views 340,357
Bookmarked 115 times

Simple HTTP Client using WININET

By | 2 Sep 2003 | Article
Simple HTTP Client, HTTP GET, HTTP POST, HTTP POST-MultiPartFormData

Introduction

This class is used for HTTP REQUEST. It supports HTTP GET, HTTP POST and HTTP POST-MultiPartFormData.

Class Overview

class GenericHTTPClient {
public:                    
    ...
    // CONSTRUCTOR & DESTRUCTOR
    GenericHTTPClient();
    virtual ~GenericHTTPClient();

    ...
    // Connection handler    
    BOOL Connect(    LPCTSTR szAddress,
            LPCTSTR szAgent = __DEFAULT_AGENT_NAME,
            unsigned short nPort = INTERNET_DEFAULT_HTTP_PORT,
            LPCTSTR szUserAccount = NULL,
            LPCTSTR szPassword = NULL);

    BOOL Close();
    VOID InitilizePostArguments();

    // HTTP Arguments handler    
    VOID AddPostArguments(LPCTSTR szName, DWORD nValue);
    VOID AddPostArguments(LPCTSTR szName, LPCTSTR szValue, 
      BOOL bBinary = FALSE);

    // HTTP Method handler 
    BOOL Request(    LPCTSTR szURL,
            int nMethod = GenericHTTPClient::RequestGetMethod,
            LPCTSTR szAgent = __DEFAULT_AGENT_NAME);

    BOOL RequestOfURI(LPCTSTR szURI, int nMethod = 
           GenericHTTPClient::RequestGetMethod);
    BOOL Response(PBYTE pHeaderBuffer, DWORD dwHeaderBufferLength, 
           PBYTE pBuffer, DWORD dwBufferLength, DWORD &dwResultSize);    
    LPCTSTR QueryHTTPResponse();
    LPCTSTR QueryHTTPResponseHeader();    

    // General Handler
    DWORD GetLastError();
    LPCTSTR GetContentType(LPCTSTR szName);
    VOID ParseURL(LPCTSTR szURL, LPTSTR szProtocol, LPTSTR szAddress, 
          DWORD &dwPort, LPTSTR szURI);
    
protected:                
    ...
};
  • Connect(...) method connects to HTTP Server.
  • Close() method closes connection. These are used with RequestOfURI(...).
  • InitilizePostArguments() method initializes post arguments.
  • AddPostArguments(...) method is supported so that you can add new post arguments of the following 3 types ( TCHAR, DWORD, FILE).
  • Request(...) method is for you to attempt request for HTTP REQUEST( GET, POST, POST-MULTIPARTFORMDATA) with URL. HTTP METHOD indirector have 3 types.
    • GenericHTTPClient::GetMethod is HTTP GET REQUEST
    • GenericHTTPClient::PostMethod is HTTP POST REQUEST
    • GenericHTTPClient::PostMultiPartsFormData is HTTP POST REQUEST with BINARY FORM DATA
  • RequestOfURI(...) method is that you could have attempt request for HTTP REQUEST with URI. This method is used with Connect(...) and Close().
  • Response(...) method is that you have HTTP Response by BYTES.
  • QueryHTTPResponse() method is you have receive HTML of your HTTP REQUEST.
  • QueryHTTPResponseHeader() method is you have receive HTTP HEADER about QueryHTTPResponse().
  • GetLastError() method is you get windows error code.
  • GetContentType(...) method is you have get MIME-TYPE.
  • ParseURL(...) method parse URL to protocol(HTTP, HTTPS) and address(or hostname) and port, URI.

Usage

Now, you have to simply do  HTTP GET REQUEST iteration.

....

GenericHTTPClient    httpClient;

if(httpRequest.Request("http://www.codeproject.com")){
    LPCTSTR szHTML=httpRequest.QueryResponse();
}else{
    LPVOID     lpMsgBuffer;
    DWORD dwRet=FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | 
             FORMAT_MESSAGE_FROM_SYSTEM,
             NULL,
             httpRequest.GetLastError(),
             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
             reinterpret_cast<LPTSTR>(&lpMsgBuffer),
             0,
             NULL);                         

    MessageBox( reinterpret_cast<LPTSTR>(lpMsgBuffer), "ERROR", MB_OK);
    LocalFree(lpMsgBuffer);
}

This is HTTP POST REQUEST with file posting ( HTTP POST Multipart/form-data)

GenericHTTPClient *pClient=new GenericHTTPClient();

    pClient->InitilizePostArguments();
    pClient->AddPostArguments(__TAG_USRID, szUserID);
    pClient->AddPostArguments(__TAG_JUMIN, szSocialIndex);
    pClient->AddPostArguments(__TAG_SRC, szSource);
    pClient->AddPostArguments(__TAG_DST, szDestination);            
    pClient->AddPostArguments(__TAG_FORMAT, szFormat);
    pClient->AddPostArguments(__TAG_SUBJECT, szMessage);

    if(bCharge){
        pClient->AddPostArguments(__TAG_CHARGE, "Y");
    }else{
        pClient->AddPostArguments(__TAG_CHARGE, "N");
    }
    pClient->AddPostArguments(__TAG_CPCODE, szCPCode);
    pClient->AddPostArguments(__TAG_FILE, szFile, TRUE);

    if(pClient->Request(szURL, 
        GenericHTTPClient::RequestPostMethodMultiPartsFormData)){        
        LPCTSTR szResult=pClient->QueryHTTPResponse();
    }else{
#ifdef    _DEBUG
        LPVOID     lpMsgBuffer;
        DWORD dwRet=FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
                      FORMAT_MESSAGE_FROM_SYSTEM,
                      NULL,
                      pClient->GetLastError(),
                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                      reinterpret_cast<LPTSTR>(&lpMsgBuffer),
                      0,
                      NULL);
        OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
        LocalFree(lpMsgBuffer);
#endif
    }

}

Thanks

Thanks that you have been reading my article and my bad English. ;p

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Yongseon Heo

Software Developer

Korea (Republic Of) Korea (Republic Of)

Member

Poke tongue | ;-P


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionMany and many ERRORS.... PinmemberAric Green16:39 12 Sep '11  
GeneralMy vote of 5 Pinmemberwilliam cc16:57 6 Oct '10  
GeneralUnable to compile using VC 2005 PinmemberMichael B Pliam6:34 23 Jun '09  
GeneralRe: Unable to compile using VC 2005 PinmemberMichael B Pliam6:38 23 Jun '09  
GeneralError in Release mode PinmemberMember 332812522:57 4 Jun '09  
GeneralMy vote of 2 PinmemberKarstenK21:01 26 Jan '09  
QuestionHttpOpenRequest GetLastError return 122 Pinmembercaoxin.wh18:00 22 Dec '08  
Generalcan't compile Pinmembergu@z16:05 12 Nov '08  
GeneralAdding headers PinmemberNirav Doshi9:06 29 Jun '08  
Hi Yongseon,
 
Well written class! Thanks for the same!
 
You've not added any methods to add custom headers to be sent to websites. Any suggestions how I can handle the same, extend your class to allow the same? (Sorry - I'm new to WinINET - so any 'pointers' will help me follow the right direction.)
 
Also, if you have an updated version of your class, can you please direct me to it?
 
Thanks again,
 
Nirav Doshi
 
* Blood donation is the superior'est form of donation, it can save lives. Celebrate special occasions, like your birthdays, by donating blood. *

AnswerRe: Adding headers [modified] PinmemberNirav Doshi1:49 2 Jul '08  
GeneralRe: Adding headers PinmemberRay Gerami10:09 27 Jul '09  
GeneralReading binary data PinmemberEdyb4:10 27 Jun '08  
GeneralUpload file with size greater that 3MB Pinmemberhishree21:06 24 Jun '08  
QuestionSupport for HTTPS/SSL PinmemberDavidJHubbard5:08 17 Jun '08  
Generalinvalid argument internetclosehandle argument1 hinternet:0x00cc0004 Pinmembermicosun8:11 29 Apr '08  
QuestionWhy does the CheckConnectionExists() function fail when there is clearly an Internet Connection? PinmemberMichael B Pliam17:31 24 Apr '08  
QuestionHelp about Upload the File(How to set the parameter) Pinmemberhishree20:36 16 Mar '08  
GeneralSimple HTTP Client using WININET PinmemberAsia_chenchen5:51 21 Sep '07  
Questionwhat is the usage 0f Response? Pinmembersunwayland17:12 9 Jul '07  
Questionhelp me,how to send this post? Pinmembershellhy0:40 4 Jul '07  
GeneralBytes sent / received Pinmember__TSX__2:02 23 Apr '07  
Generalsuck Pinmember_ppr0:55 10 Dec '06  
GeneralRe: suck PinmemberH.J.Park16:55 26 Jul '11  
QuestionUnhandled exception at 0x7c84b273 in myProject.exe: 0xC0000005: Access violation reading location 0xcccccccc. Pinmemberjuhtzyy20:02 31 Oct '06  
AnswerRe: Unhandled exception at 0x7c84b273 in myProject.exe: 0xC0000005: Access violation reading location 0xcccccccc. PinmemberMichael B Pliam16:16 1 Dec '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120529.1 | Last Updated 3 Sep 2003
Article Copyright 2003 by Yongseon Heo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid