基於WININET的簡單C++ Http類

來源:互聯網
上載者:User

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

Simple HTTP Client using WININET

簡介

這個類是用於HTTP請求。它支援的HTTP GET,HTTP POST和HTTP的POST-
MultiPartFormData。

類概述

撤消修改
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.

 

使用

現在,你有根本的HTTP GET請求的迭代。

....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) 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    }}

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.