Simple C ++ HTTP Class Based on wininet

Source: Internet
Author: User

Http://www.codeproject.com/KB/IP/simplehttpclient.aspx

Simple HTTP client using wininet

Introduction

This class is used for HTTP requests. It supports http get, http post, and http post-
Multipartformdata.

Class Overview

Undo Modification
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 cocould 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.

 

Use

Now, you have a fundamental 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    }}

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.