Click here to Skip to main content
Click here to Skip to main content

Simple HTTP Client using WININET

By , 2 Sep 2003
 

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionStill one of the best free emailers out there - but can't get it working in UnicodememberMichael B Pliam29 Jan '13 - 18:36 
QuestionMany and many ERRORS....memberAric Green12 Sep '11 - 16:39 
GeneralMy vote of 5memberwilliam cc6 Oct '10 - 16:57 
GeneralUnable to compile using VC 2005memberMichael B Pliam23 Jun '09 - 6:34 
GeneralRe: Unable to compile using VC 2005memberMichael B Pliam23 Jun '09 - 6:38 
GeneralError in Release modememberMember 33281254 Jun '09 - 22:57 
GeneralMy vote of 2memberKarstenK26 Jan '09 - 21:01 
QuestionHttpOpenRequest GetLastError return 122membercaoxin.wh22 Dec '08 - 18:00 
Generalcan't compilemembergu@z12 Nov '08 - 16:05 
GeneralAdding headersmemberNirav Doshi29 Jun '08 - 9:06 
AnswerRe: Adding headers [modified]memberNirav Doshi2 Jul '08 - 1:49 
GeneralRe: Adding headersmemberRay Gerami27 Jul '09 - 10:09 
GeneralReading binary datamemberEdyb27 Jun '08 - 4:10 
GeneralUpload file with size greater that 3MBmemberhishree24 Jun '08 - 21:06 
QuestionSupport for HTTPS/SSLmemberDavidJHubbard17 Jun '08 - 5:08 
Generalinvalid argument internetclosehandle argument1 hinternet:0x00cc0004membermicosun29 Apr '08 - 8:11 
QuestionWhy does the CheckConnectionExists() function fail when there is clearly an Internet Connection?memberMichael B Pliam24 Apr '08 - 17:31 
QuestionHelp about Upload the File(How to set the parameter)memberhishree16 Mar '08 - 20:36 
GeneralSimple HTTP Client using WININETmemberAsia_chenchen21 Sep '07 - 5:51 
Questionwhat is the usage 0f Response?membersunwayland9 Jul '07 - 17:12 
Questionhelp me,how to send this post?membershellhy4 Jul '07 - 0:40 
GeneralBytes sent / receivedmember__TSX__23 Apr '07 - 2:02 
Generalsuckmember_ppr10 Dec '06 - 0:55 
GeneralRe: suckmemberH.J.Park26 Jul '11 - 16:55 
QuestionUnhandled exception at 0x7c84b273 in myProject.exe: 0xC0000005: Access violation reading location 0xcccccccc.memberjuhtzyy31 Oct '06 - 20:02 
AnswerRe: Unhandled exception at 0x7c84b273 in myProject.exe: 0xC0000005: Access violation reading location 0xcccccccc.memberMichael B Pliam1 Dec '06 - 16:16 
GeneralRe: Unhandled exception at 0x7c84b273 in myProject.exe: 0xC0000005: Access violation reading location 0xcccccccc.memberEarnol23 Apr '07 - 0:03 
GeneralRe: Unhandled exception at 0x7c84b273 in myProject.exe: 0xC0000005: Access violation reading location 0xcccccccc.memberkhanathar78626 Nov '07 - 4:37 
QuestionA HTTPSendRequest Problemmemberjiangrenkuan26 Oct '06 - 21:31 
GeneralRe: A HTTPSendRequest Problemmemberraghup1326 Oct '06 - 23:34 
AnswerRe: A HTTPSendRequest Problemmembershellhy22 Jul '07 - 3:10 
GeneralError LNK2005memberAlex_j2 Sep '06 - 2:37 
Generalthe SESSIONID can't changemembershellhy14 Aug '06 - 7:54 
QuestionHow to to POST Data?memberAdeel6887 Aug '06 - 2:35 
GeneralCan't compilememberAdeel6887 Aug '06 - 2:24 
GeneralRe: Can't compilememberandriuha13 Sep '06 - 2:14 
GeneralNeed help in uploading imagememberranu_hai28 Jul '06 - 0:02 
Generalplease give me a next versionmembershellhy1 Jun '06 - 1:42 
QuestionHow do you format a POST string to produce a line break in the HTML output? [modified]memberMichael B Pliam26 May '06 - 7:13 
GeneralProblem with AddPostArgumentsmemberMichael B Pliam22 May '06 - 4:48 
GeneralRe: Problem with AddPostArgumentsmemberMike Pliam22 May '06 - 13:13 
GeneralRe: solutionmemberarieharieh26 May '06 - 1:51 
GeneralRe: solutionmemberMichael B Pliam26 May '06 - 6:46 
GeneralIt's nice module But found one Error maybe..membernami034218 May '06 - 2:46 
GeneralError C2065 : "httpRequest" : no declaration identifiermemberlangzi011526 Apr '06 - 18:56 
GeneralRe: Error C2065 : "httpRequest" : no declaration identifiermemberMember 853086830 Jan '12 - 3:39 
Generalcan't compilemembernubz22 Mar '06 - 0:37 
GeneralRe: can't compilememberJust do it4 Apr '06 - 5:33 
QuestionHelp - Login to internet sitesmemberddalmia20008 Dec '05 - 17:28 
AnswerRe: Help - Login to internet sitesmemberYongseon Heo19 Dec '05 - 17:11 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 3 Sep 2003
Article Copyright 2003 by Yongseon Heo
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid