Click here to Skip to main content
15,890,845 members
Articles / Desktop Programming / MFC
Article

Simple HTTP Client using WININET

Rate me:
Please Sign up or sign in to vote.
3.76/5 (46 votes)
2 Sep 20031 min read 590.4K   13.2K   145   167
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

C++
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.

C++
....

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)

C++
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


Written By
Software Developer
Korea (Republic of) Korea (Republic of)
Poke tongue | ;-P

Comments and Discussions

 
General12 errors &quot;unresolved external symbol&quot; Pin
anynoums29-Jul-05 21:52
sussanynoums29-Jul-05 21:52 
GeneralRe: 12 errors &quot;unresolved external symbol&quot; Pin
Heo Yongseon31-Jul-05 17:59
Heo Yongseon31-Jul-05 17:59 
GeneralThanx Pin
Anonymous31-Jul-05 23:10
Anonymous31-Jul-05 23:10 
Generalmsvcrtd.lib(crtexe.obj) : error LNK2001: unresolved external symbol _main Pin
Girish6016-Apr-06 0:07
Girish6016-Apr-06 0:07 
GeneralRe: msvcrtd.lib(crtexe.obj) : error LNK2001: unresolved external symbol _main Pin
blitz_cn27-Mar-07 1:13
blitz_cn27-Mar-07 1:13 
Questioncan HttpSendRequest accept UNICODE for POST arguments? Pin
big_marvin27-Jul-05 8:12
big_marvin27-Jul-05 8:12 
GeneralPosting of userID password for authentication Pin
bouli291023-May-05 19:36
bouli291023-May-05 19:36 
GeneralRe: Posting of userID password for authentication Pin
Heo Yongseon24-May-05 14:07
Heo Yongseon24-May-05 14:07 
http://www.codeproject.com/internet/w3client.asp[^]

maybe, this article help u.

Smile | :)

Poke tongue | ;-P
GeneralXML File Pin
Elizabete6-May-05 2:17
Elizabete6-May-05 2:17 
GeneralRe: XML File Pin
Heo Yongseon8-May-05 15:08
Heo Yongseon8-May-05 15:08 
GeneralXML Data Pin
achile0074-May-05 23:49
achile0074-May-05 23:49 
GeneralRe: XML Data Pin
kim jun ho6-Jul-05 15:34
kim jun ho6-Jul-05 15:34 
QuestionHow to Post XML file Pin
dchauhan24-Mar-05 22:21
dchauhan24-Mar-05 22:21 
AnswerRe: How to Post XML file Pin
Heo Yongseon31-Mar-05 14:07
Heo Yongseon31-Mar-05 14:07 
GeneralStack corruption during non-multipart post Pin
Anonymous15-Mar-05 5:29
Anonymous15-Mar-05 5:29 
GeneralRe: Stack corruption during non-multipart post Pin
Heo Yongseon31-Mar-05 13:59
Heo Yongseon31-Mar-05 13:59 
GeneralRe: Stack corruption during non-multipart post Pin
nguyendhex4-Apr-05 0:24
nguyendhex4-Apr-05 0:24 
GeneralRe: Stack corruption during non-multipart post Pin
Heo Yongseon4-Apr-05 2:38
Heo Yongseon4-Apr-05 2:38 
GeneralOne bug on this class Pin
Laurentiu Soica14-Mar-05 2:50
Laurentiu Soica14-Mar-05 2:50 
GeneralRe: One bug on this class Pin
Heo Yongseon31-Mar-05 13:55
Heo Yongseon31-Mar-05 13:55 
GeneralPost Pin
Y G11-Mar-05 10:04
Y G11-Mar-05 10:04 
GeneralUnresolved externals Pin
Member 86764822-Feb-05 10:23
Member 86764822-Feb-05 10:23 
GeneralRe: Unresolved externals Pin
Member 86764823-Feb-05 4:42
Member 86764823-Feb-05 4:42 
GeneralSSL won't work, workaround included Pin
ericb_summit1-Feb-05 12:19
ericb_summit1-Feb-05 12:19 
QuestionDoes it support setting cookies to url? Pin
cop115-Jan-05 7:37
cop115-Jan-05 7:37 

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.