Click here to Skip to main content
15,889,552 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.2K   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

 
GeneralRuntime overflow - suggestion Pin
Sina Falahati25-Dec-04 4:05
Sina Falahati25-Dec-04 4:05 
GeneralRe: Runtime overflow - suggestion Pin
Heo Yongseon29-Dec-04 13:45
Heo Yongseon29-Dec-04 13:45 
QuestionCan it be use with Borland C++ Builder Pin
bryu29-Nov-04 21:49
bryu29-Nov-04 21:49 
Generalhas some spell problem in explations. Pin
jakejang25-Aug-04 16:44
jakejang25-Aug-04 16:44 
GeneralRe: has some spell problem in explations. Pin
Heo Yongseon26-Aug-04 16:41
Heo Yongseon26-Aug-04 16:41 
GeneralGetting problem on Windows XP Pin
Sumit Kapoor12-Aug-04 23:47
Sumit Kapoor12-Aug-04 23:47 
GeneralRe: Getting problem on Windows XP Pin
Heo Yongseon17-Aug-04 16:00
Heo Yongseon17-Aug-04 16:00 
GeneralRe: Getting problem on Windows XP Pin
Sumit Kapoor19-Aug-04 7:49
Sumit Kapoor19-Aug-04 7:49 
Hi Dear,
I'm 24/m/india. I love to experiment with VC++, e.g.: login site through VC++ & Parse page & find out needed information.

I solved the problem that I've specified last time, that was due to cookies.
when I tried Login a site then if I fire right request it genrate cookies on my PC. & if I direct call a page without login page then it start finding old cookies. if such a condition(system check cookie on your PC) come then HTTPEndRequest() function fail if there is no right cookie there.

So solution is try to Login & then fire next request, so that server genrate cookie on your PC.

I hope u understand what I means to say...not understand? then read again...

All I do is experiment with your and others code to keep in mind that some day I'll deveolop something BIG.

thanks for support, Have a Nice Life..
Bye see U soon.
Sumit Kapoor Smile | :)
sumit_kapooor1980@hotmail.com

Never consider anything Impossible before trying to solve that..---Sumit Kapoor---
GeneralRe: Getting problem on Windows XP Pin
Heo Yongseon19-Aug-04 16:11
Heo Yongseon19-Aug-04 16:11 
GeneralGenericHTTPClient::Request will not return Pin
Member 13407119-Jun-04 15:44
Member 13407119-Jun-04 15:44 
GeneralRe: GenericHTTPClient::Request will not return Pin
Heo Yongseon9-Jun-04 16:12
Heo Yongseon9-Jun-04 16:12 
GeneralSumit: How to Download File using http Pin
Sumit Kapoor28-May-04 21:22
Sumit Kapoor28-May-04 21:22 
GeneralRe: Sumit: How to Download File using http Pin
Heo Yongseon30-May-04 14:55
Heo Yongseon30-May-04 14:55 
GeneralSumit again with one problem: Pin
Sumit Kapoor18-May-04 19:07
Sumit Kapoor18-May-04 19:07 
GeneralRe: Sumit again with one problem: Pin
Heo Yongseon30-May-04 14:51
Heo Yongseon30-May-04 14:51 
GeneralUsingTo Chck Internet Connection Pin
Jigbesh4-May-04 19:22
Jigbesh4-May-04 19:22 
Generalnew article W3Client Pin
Heo Yongseon3-May-04 23:29
Heo Yongseon3-May-04 23:29 
GeneralAllocMultiPartsFormData Pin
Anonymous2-May-04 22:43
Anonymous2-May-04 22:43 
GeneralPLAIN TEXT VERSUS HTML Pin
tyounsi28-Apr-04 2:05
tyounsi28-Apr-04 2:05 
GeneralProxy Server Pin
tyounsi26-Apr-04 2:24
tyounsi26-Apr-04 2:24 
GeneralRe: Proxy Server Pin
Heo Yongseon26-Apr-04 13:57
Heo Yongseon26-Apr-04 13:57 
GeneralRe: Proxy Server Pin
Anonymous2-Dec-04 1:42
Anonymous2-Dec-04 1:42 
Generalprogression callback Pin
tyounsi16-Apr-04 9:39
tyounsi16-Apr-04 9:39 
GeneralRe: progression callback Pin
Heo Yongseon17-Apr-04 0:15
Heo Yongseon17-Apr-04 0:15 
GeneralRe: progression callback Pin
tyounsi17-Apr-04 1:28
tyounsi17-Apr-04 1:28 

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.