{"rsdb":{"rid":"16133","subhead":"","postdate":"0","aid":"12010","fid":"56","uid":"1","topic":"1","content":"

 \u5f53\u4f7f\u7528C++<\/a>\u505aHTTP\u5ba2\u6237\u7aef\u65f6\uff0c\u76ee\u524d\u901a\u7528\u7684\u505a\u6cd5\u5c31\u662f\u4f7f\u7528libcurl\u3002\u5176\u5b98\u65b9\u7f51\u7ad9\u7684\u5730\u5740\u662fhttp:\/\/curl.haxx.se\/\uff0c\u8be5\u7f51\u7ad9\u4e3b\u8981\u63d0\u4f9b\u4e86Curl\u548clibcurl\u3002Curl\u662f\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u7528\u4e8e\u5b8c\u6210FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE \u4ee5\u53ca LDAP\u7684\u547d\u4ee4\u7684\u8bf7\u6c42\u53ca\u63a5\u6536\u56de\u9988\u3002libcurl\u63d0\u4f9b\u7ed9\u5f00\u53d1\u8005\uff0c\u7528\u4e8e\u4f7f\u7528C++<\/a>\u8de8\u5e73\u53f0\u7684\u5f00\u53d1\u5404\u79cd\u7f51\u7edc\u534f\u8bae\u7684\u8bf7\u6c42\u53ca\u54cd\u5e94\u3002\u91cc\u9762\u7684\u6587\u6863\u975e\u5e38\u9f50\u5168\uff0c\u4e0d\u8fc7\u90fd\u662f\u82f1\u6587\u7684\u3002<\/p>

    \u672c\u6587\u63d0\u4f9b\u6700\u7b80\u5355\u7684demo\u4f7f\u7528libcurl\u5f00\u53d1HttpClient\u3002\u4e3b\u8981\u5305\u62ec\u540c\u6b65\u7684HTTP GET\u3001HTTP POST\u3001HTTPS GET\u3001HTTPS POST\u3002<\/p>

    \u4e0b\u8f7dlibcurl\u5305\uff0c\u5982\u679c\u4f7f\u7528Linux\u5e73\u53f0\uff0c\u5efa\u8bae\u4e0b\u8f7d\u6e90\u6587\u4ef6\u7f16\u8bd1\uff1b\u5982\u679c\u4f7f\u7528Windows\u5e73\u53f0\uff0c\u5efa\u8bae\u4e0b\u8f7dWin32 - MSVC\uff0c\u4e0b\u8f7d\u5730\u5740\u662f\uff1ahttp:\/\/curl.haxx.se\/download.html
<\/p>

<\/p>

[cpp]<\/strong> view plain<\/a>copy<\/a>
<\/div><\/div><\/div>
  1. #ifndef __HTTP_CURL_H__<\/span>  <\/span><\/span><\/p><\/li>

  2. #define __HTTP_CURL_H__<\/span>  <\/span><\/span><\/p><\/li>

  3.   <\/span><\/p><\/li>

  4. #include <string><\/span>  <\/span><\/span><\/p><\/li>

  5.   <\/span><\/p><\/li>

  6. class<\/span> CHttpClient  <\/span><\/span><\/p><\/li>

  7. {  <\/span><\/p><\/li>

  8. public<\/span>:  <\/span><\/span><\/p><\/li>

  9.     CHttpClient(void<\/span>);  <\/span><\/span><\/p><\/li>

  10.     ~CHttpClient(void<\/span>);  <\/span><\/span><\/p><\/li>

  11.   <\/span><\/p><\/li>

  12. public<\/span>:  <\/span><\/span><\/p><\/li>

  13.     \/**<\/span> <\/span><\/p><\/li>

  14.     * @brief HTTP POST\u8bf7\u6c42<\/span> <\/span><\/p><\/li>

  15.     * @param strUrl \u8f93\u5165\u53c2\u6570,\u8bf7\u6c42\u7684Url\u5730\u5740,\u5982:http:\/\/www.baidu.com<\/span> <\/span><\/p><\/li>

  16.     * @param strPost \u8f93\u5165\u53c2\u6570,\u4f7f\u7528\u5982\u4e0b\u683c\u5f0fpara1=val1¶2=val2&\u2026<\/span> <\/span><\/p><\/li>

  17.     * @param strResponse \u8f93\u51fa\u53c2\u6570,\u8fd4\u56de\u7684\u5185\u5bb9<\/span> <\/span><\/p><\/li>

  18.     * @return \u8fd4\u56de\u662f\u5426Post\u6210\u529f<\/span> <\/span><\/p><\/li>

  19.     *\/<\/span>  <\/span><\/span><\/p><\/li>

  20.     int<\/span> Post(<\/span>const<\/span> std::string & strUrl, <\/span>const<\/span> std::string & strPost, std::string & strResponse);  <\/span><\/span><\/p><\/li>

  21.   <\/span><\/p><\/li>

  22.     \/**<\/span> <\/span><\/p><\/li>

  23.     * @brief HTTP GET\u8bf7\u6c42<\/span> <\/span><\/p><\/li>

  24.     * @param strUrl \u8f93\u5165\u53c2\u6570,\u8bf7\u6c42\u7684Url\u5730\u5740,\u5982:http:\/\/www.baidu.com<\/span> <\/span><\/p><\/li>

  25.     * @param strResponse \u8f93\u51fa\u53c2\u6570,\u8fd4\u56de\u7684\u5185\u5bb9<\/span> <\/span><\/p><\/li>

  26.     * @return \u8fd4\u56de\u662f\u5426Post\u6210\u529f<\/span> <\/span><\/p><\/li>

  27.     *\/<\/span>  <\/span><\/span><\/p><\/li>

  28.     int<\/span> Get(<\/span>const<\/span> std::string & strUrl, std::string & strResponse);  <\/span><\/span><\/p><\/li>

  29.   <\/span><\/p><\/li>

  30.     \/**<\/span> <\/span><\/p><\/li>

  31.     * @brief HTTPS POST\u8bf7\u6c42,\u65e0\u8bc1\u4e66\u7248\u672c<\/span> <\/span><\/p><\/li>

  32.     * @param strUrl \u8f93\u5165\u53c2\u6570,\u8bf7\u6c42\u7684Url\u5730\u5740,\u5982:https:\/\/www.alipay.com<\/span> <\/span><\/p><\/li>

  33.     * @param strPost \u8f93\u5165\u53c2\u6570,\u4f7f\u7528\u5982\u4e0b\u683c\u5f0fpara1=val1¶2=val2&\u2026<\/span> <\/span><\/p><\/li>

  34.     * @param strResponse \u8f93\u51fa\u53c2\u6570,\u8fd4\u56de\u7684\u5185\u5bb9<\/span> <\/span><\/p><\/li>

  35.     * @param pCaPath \u8f93\u5165\u53c2\u6570,\u4e3aCA\u8bc1\u4e66\u7684\u8def\u5f84.\u5982\u679c\u8f93\u5165\u4e3aNULL,\u5219\u4e0d\u9a8c\u8bc1\u670d\u52a1\u5668\u7aef\u8bc1\u4e66\u7684\u6709\u6548\u6027.<\/span> <\/span><\/p><\/li>

  36.     * @return \u8fd4\u56de\u662f\u5426Post\u6210\u529f<\/span> <\/span><\/p><\/li>

  37.     *\/<\/span>  <\/span><\/span><\/p><\/li>

  38.     int<\/span> Posts(<\/span>const<\/span> std::string & strUrl, <\/span>const<\/span> std::string & strPost, std::string & strResponse, <\/span>const<\/span> <\/span>char<\/span> * pCaPath = NULL);  <\/span><\/span><\/p><\/li>

  39.   <\/span><\/p><\/li>

  40.     \/**<\/span> <\/span><\/p><\/li>

  41.     * @brief HTTPS GET\u8bf7\u6c42,\u65e0\u8bc1\u4e66\u7248\u672c<\/span> <\/span><\/p><\/li>

  42.     * @param strUrl \u8f93\u5165\u53c2\u6570,\u8bf7\u6c42\u7684Url\u5730\u5740,\u5982:https:\/\/www.alipay.com<\/span> <\/span><\/p><\/li>

  43.     * @param strResponse \u8f93\u51fa\u53c2\u6570,\u8fd4\u56de\u7684\u5185\u5bb9<\/span> <\/span><\/p><\/li>

  44.     * @param pCaPath \u8f93\u5165\u53c2\u6570,\u4e3aCA\u8bc1\u4e66\u7684\u8def\u5f84.\u5982\u679c\u8f93\u5165\u4e3aNULL,\u5219\u4e0d\u9a8c\u8bc1\u670d\u52a1\u5668\u7aef\u8bc1\u4e66\u7684\u6709\u6548\u6027.<\/span> <\/span><\/p><\/li>

  45.     * @return \u8fd4\u56de\u662f\u5426Post\u6210\u529f<\/span> <\/span><\/p><\/li>

  46.     *\/<\/span>  <\/span><\/span><\/p><\/li>

  47.     int<\/span> Gets(<\/span>const<\/span> std::string & strUrl, std::string & strResponse, <\/span>const<\/span> <\/span>char<\/span> * pCaPath = NULL);  <\/span><\/span><\/p><\/li>

  48.   <\/span><\/p><\/li>

  49. public<\/span>:  <\/span><\/span><\/p><\/li>

  50.     void<\/span> SetDebug(<\/span>bool<\/span> bDebug);  <\/span><\/span><\/p><\/li>

  51.   <\/span><\/p><\/li>

  52. private<\/span>:  <\/span><\/span><\/p><\/li>

  53.     bool<\/span> m_bDebug;  <\/span><\/span><\/p><\/li>

  54. };  <\/span><\/p><\/li>

  55.   <\/span><\/p><\/li>

  56. #endif<\/span>  <\/span><\/span><\/p><\/li><\/ol><\/div>

    [cpp]<\/strong> view plain<\/a>copy<\/a>
    <\/div><\/div><\/div>
    1. #include "httpclient.h"<\/span>  <\/span><\/span><\/p><\/li>

    2. #include "curl\/curl.h"<\/span>  <\/span><\/span><\/p><\/li>

    3. #include <string><\/span>  <\/span><\/span><\/p><\/li>

    4.   <\/span><\/p><\/li>

    5. CHttpClient::CHttpClient(void<\/span>) :   <\/span><\/span><\/p><\/li>

    6. m_bDebug(false<\/span>)  <\/span><\/span><\/p><\/li>

    7. {  <\/span><\/p><\/li>

    8.   <\/span><\/p><\/li>

    9. }  <\/span><\/p><\/li>

    10.   <\/span><\/p><\/li>

    11. CHttpClient::~CHttpClient(void<\/span>)  <\/span><\/span><\/p><\/li>

    12. {  <\/span><\/p><\/li>

    13.   <\/span><\/p><\/li>

    14. }  <\/span><\/p><\/li>

    15.   <\/span><\/p><\/li>

    16. static<\/span> <\/span>int<\/span> OnDebug(CURL *, curl_infotype itype, <\/span>char<\/span> * pData, <\/span>size_t<\/span> size, <\/span>void<\/span> *)  <\/span><\/span><\/p><\/li>

    17. {  <\/span><\/p><\/li>

    18.     if<\/span>(itype == CURLINFO_TEXT)  <\/span><\/span><\/p><\/li>

    19.     {  <\/span><\/p><\/li>

    20.         \/\/printf("[TEXT]%s\\n", pData);<\/span>  <\/span><\/span><\/p><\/li>

    21.     }  <\/span><\/p><\/li>

    22.     else<\/span> <\/span>if<\/span>(itype == CURLINFO_HEADER_IN)  <\/span><\/span><\/p><\/li>

    23.     {  <\/span><\/p><\/li>

    24.         printf("[HEADER_IN]%s\\n"<\/span>, pData);  <\/span><\/span><\/p><\/li>

    25.     }  <\/span><\/p><\/li>

    26.     else<\/span> <\/span>if<\/span>(itype == CURLINFO_HEADER_OUT)  <\/span><\/span><\/p><\/li>

    27.     {  <\/span><\/p><\/li>

    28.         printf("[HEADER_OUT]%s\\n"<\/span>, pData);  <\/span><\/span><\/p><\/li>

    29.     }  <\/span><\/p><\/li>

    30.     else<\/span> <\/span>if<\/span>(itype == CURLINFO_DATA_IN)  <\/span><\/span><\/p><\/li>

    31.     {  <\/span><\/p><\/li>

    32.         printf("[DATA_IN]%s\\n"<\/span>, pData);  <\/span><\/span><\/p><\/li>

    33.     }  <\/span><\/p><\/li>

    34.     else<\/span> <\/span>if<\/span>(itype == CURLINFO_DATA_OUT)  <\/span><\/span><\/p><\/li>

    35.     {  <\/span><\/p><\/li>

    36.         printf("[DATA_OUT]%s\\n"<\/span>, pData);  <\/span><\/span><\/p><\/li>

    37.     }  <\/span><\/p><\/li>

    38.     return<\/span> 0;  <\/span><\/span><\/p><\/li>

    39. }  <\/span><\/p><\/li>

    40.   <\/span><\/p><\/li>

    41. static<\/span> <\/span>size_t<\/span> OnWriteData(<\/span>void<\/span>* buffer, <\/span>size_t<\/span> size, <\/span>size_t<\/span> nmemb, <\/span>void<\/span>* lpVoid)  <\/span><\/span><\/p><\/li>

    42. {  <\/span><\/p><\/li>

    43.     std::string* str = dynamic_cast<\/span><std::string*>((std::string *)lpVoid);  <\/span><\/span><\/p><\/li>

    44.     if<\/span>( NULL == str || NULL == buffer )  <\/span><\/span><\/p><\/li>

    45.     {  <\/span><\/p><\/li>

    46.         return<\/span> -1;  <\/span><\/span><\/p><\/li>

    47.     }  <\/span><\/p><\/li>

    48.   <\/span><\/p><\/li>

    49.     char<\/span>* pData = (<\/span>char<\/span>*)buffer;  <\/span><\/span><\/p><\/li>

    50.     str->append(pData, size * nmemb);  <\/span><\/p><\/li>

    51.     return<\/span> nmemb;  <\/span><\/span><\/p><\/li>

    52. }  <\/span><\/p><\/li>

    53.   <\/span><\/p><\/li>

    54. int<\/span> CHttpClient::Post(<\/span>const<\/span> std::string & strUrl, <\/span>const<\/span> std::string & strPost, std::string & strResponse)  <\/span><\/span><\/p><\/li>

    55. {  <\/span><\/p><\/li>

    56.     CURLcode res;  <\/span><\/p><\/li>

    57.     CURL* curl = curl_easy_init();  <\/span><\/p><\/li>

    58.     if<\/span>(NULL == curl)  <\/span><\/span><\/p><\/li>

    59.     {  <\/span><\/p><\/li>

    60.         return<\/span> CURLE_FAILED_INIT;  <\/span><\/span><\/p><\/li>

    61.     }  <\/span><\/p><\/li>

    62.     if<\/span>(m_bDebug)  <\/span><\/span><\/p><\/li>

    63.     {  <\/span><\/p><\/li>

    64.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  <\/span><\/p><\/li>

    65.         curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  <\/span><\/p><\/li>

    66.     }  <\/span><\/p><\/li>

    67.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  <\/span><\/p><\/li>

    68.     curl_easy_setopt(curl, CURLOPT_POST, 1);  <\/span><\/p><\/li>

    69.     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());  <\/span><\/p><\/li>

    70.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  <\/span><\/p><\/li>

    71.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  <\/span><\/p><\/li>

    72.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void<\/span> *)&strResponse);  <\/span><\/span><\/p><\/li>

    73.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  <\/span><\/p><\/li>

    74.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  <\/span><\/p><\/li>

    75.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  <\/span><\/p><\/li>

    76.     res = curl_easy_perform(curl);  <\/span><\/p><\/li>

    77.     curl_easy_cleanup(curl);  <\/span><\/p><\/li>

    78.     return<\/span> res;  <\/span><\/span><\/p><\/li>

    79. }  <\/span><\/p><\/li>

    80.   <\/span><\/p><\/li>

    81. int<\/span> CHttpClient::Get(<\/span>const<\/span> std::string & strUrl, std::string & strResponse)  <\/span><\/span><\/p><\/li>

    82. {  <\/span><\/p><\/li>

    83.     CURLcode res;  <\/span><\/p><\/li>

    84.     CURL* curl = curl_easy_init();  <\/span><\/p><\/li>

    85.     if<\/span>(NULL == curl)  <\/span><\/span><\/p><\/li>

    86.     {  <\/span><\/p><\/li>

    87.         return<\/span> CURLE_FAILED_INIT;  <\/span><\/span><\/p><\/li>

    88.     }  <\/span><\/p><\/li>

    89.     if<\/span>(m_bDebug)  <\/span><\/span><\/p><\/li>

    90.     {  <\/span><\/p><\/li>

    91.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  <\/span><\/p><\/li>

    92.         curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  <\/span><\/p><\/li>

    93.     }  <\/span><\/p><\/li>

    94. <pre name="code"<\/span> <\/span>class<\/span>=<\/span>"cpp"<\/span>>   curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  <\/span><\/span><\/p><\/li>

    95.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  <\/span><\/p><\/li>

    96.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  <\/span><\/p><\/li>

    97.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void<\/span> *)&strResponse);  <\/span><\/span><\/p><\/li>

    98.     \/**<\/span> <\/span><\/p><\/li>

    99.     * \u5f53\u591a\u4e2a\u7ebf\u7a0b\u90fd\u4f7f\u7528\u8d85\u65f6\u5904\u7406\u7684\u65f6\u5019\uff0c\u540c\u65f6\u4e3b\u7ebf\u7a0b\u4e2d\u6709sleep\u6216\u662fwait\u7b49\u64cd\u4f5c\u3002<\/span> <\/span><\/p><\/li>

    100.     * \u5982\u679c\u4e0d\u8bbe\u7f6e\u8fd9\u4e2a\u9009\u9879\uff0clibcurl\u5c06\u4f1a\u53d1\u4fe1\u53f7\u6253\u65ad\u8fd9\u4e2await\u4ece\u800c\u5bfc\u81f4\u7a0b\u5e8f\u9000\u51fa\u3002<\/span> <\/span><\/p><\/li>

    101.     *\/<\/span>  <\/span><\/span><\/p><\/li>

    102.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  <\/span><\/p><\/li>

    103.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  <\/span><\/p><\/li>

    104.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  <\/span><\/p><\/li>

    105.     res = curl_easy_perform(curl);  <\/span><\/p><\/li>

    106.     curl_easy_cleanup(curl);  <\/span><\/p><\/li>

    107.     return<\/span> res;  <\/span><\/span><\/p><\/li>

    108. }  <\/span><\/p><\/li>

    109.   <\/span><\/p><\/li>

    110. int<\/span> CHttpClient::Posts(<\/span>const<\/span> std::string & strUrl, <\/span>const<\/span> std::string & strPost, std::string & strResponse, <\/span>const<\/span> <\/span>char<\/span> * pCaPath)  <\/span><\/span><\/p><\/li>

    111. {  <\/span><\/p><\/li>

    112.     CURLcode res;  <\/span><\/p><\/li>

    113.     CURL* curl = curl_easy_init();  <\/span><\/p><\/li>

    114.     if<\/span>(NULL == curl)  <\/span><\/span><\/p><\/li>

    115.     {  <\/span><\/p><\/li>

    116.         return<\/span> CURLE_FAILED_INIT;  <\/span><\/span><\/p><\/li>

    117.     }  <\/span><\/p><\/li>

    118.     if<\/span>(m_bDebug)  <\/span><\/span><\/p><\/li>

    119.     {  <\/span><\/p><\/li>

    120.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  <\/span><\/p><\/li>

    121.         curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  <\/span><\/p><\/li>

    122.     }  <\/span><\/p><\/li>

    123.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  <\/span><\/p><\/li>

    124.     curl_easy_setopt(curl, CURLOPT_POST, 1);  <\/span><\/p><\/li>

    125.     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());  <\/span><\/p><\/li>

    126.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  <\/span><\/p><\/li>

    127.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  <\/span><\/p><\/li>

    128.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void<\/span> *)&strResponse);  <\/span><\/span><\/p><\/li>

    129.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  <\/span><\/p><\/li>

    130.     if<\/span>(NULL == pCaPath)  <\/span><\/span><\/p><\/li>

    131.     {  <\/span><\/p><\/li>

    132.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false<\/span>);  <\/span><\/span><\/p><\/li>

    133.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false<\/span>);  <\/span><\/span><\/p><\/li>

    134.     }  <\/span><\/p><\/li>

    135.     else<\/span>  <\/span><\/span><\/p><\/li>

    136.     {  <\/span><\/p><\/li>

    137.         \/\/\u7f3a\u7701\u60c5\u51b5\u5c31\u662fPEM\uff0c\u6240\u4ee5\u65e0\u9700\u8bbe\u7f6e\uff0c\u53e6\u5916\u652f\u6301DER<\/span>  <\/span><\/span><\/p><\/li>

    138.         \/\/curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");<\/span>  <\/span><\/span><\/p><\/li>

    139.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true<\/span>);  <\/span><\/span><\/p><\/li>

    140.         curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);  <\/span><\/p><\/li>

    141.     }  <\/span><\/p><\/li>

    142.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  <\/span><\/p><\/li>

    143.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  <\/span><\/p><\/li>

    144.     res = curl_easy_perform(curl);  <\/span><\/p><\/li>

    145.     curl_easy_cleanup(curl);  <\/span><\/p><\/li>

    146.     return<\/span> res;  <\/span><\/span><\/p><\/li>

    147. }  <\/span><\/p><\/li>

    148.   <\/span><\/p><\/li>

    149. int<\/span> CHttpClient::Gets(<\/span>const<\/span> std::string & strUrl, std::string & strResponse, <\/span>const<\/span> <\/span>char<\/span> * pCaPath)  <\/span><\/span><\/p><\/li>

    150. {  <\/span><\/p><\/li>

    151.     CURLcode res;  <\/span><\/p><\/li>

    152.     CURL* curl = curl_easy_init();  <\/span><\/p><\/li>

    153.     if<\/span>(NULL == curl)  <\/span><\/span><\/p><\/li>

    154.     {  <\/span><\/p><\/li>

    155.         return<\/span> CURLE_FAILED_INIT;  <\/span><\/span><\/p><\/li>

    156.     }  <\/span><\/p><\/li>

    157.     if<\/span>(m_bDebug)  <\/span><\/span><\/p><\/li>

    158.     {  <\/span><\/p><\/li>

    159.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  <\/span><\/p><\/li>

    160.         curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  <\/span><\/p><\/li>

    161.     }  <\/span><\/p><\/li>

    162.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  <\/span><\/p><\/li>

    163.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  <\/span><\/p><\/li>

    164.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  <\/span><\/p><\/li>

    165.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void<\/span> *)&strResponse);  <\/span><\/span><\/p><\/li>

    166.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  <\/span><\/p><\/li>

    167.     if<\/span>(NULL == pCaPath)  <\/span><\/span><\/p><\/li>

    168.     {  <\/span><\/p><\/li>

    169.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false<\/span>);  <\/span><\/span><\/p><\/li>

    170.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false<\/span>);  <\/span><\/span><\/p><\/li>

    171.     }  <\/span><\/p><\/li>

    172.     else<\/span>  <\/span><\/span><\/p><\/li>

    173.     {  <\/span><\/p><\/li>

    174.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true<\/span>);  <\/span><\/span><\/p><\/li>

    175.         curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);  <\/span><\/p><\/li>

    176.     }  <\/span><\/p><\/li>

    177.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  <\/span><\/p><\/li>

    178.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  <\/span><\/p><\/li>

    179.     res = curl_easy_perform(curl);  <\/span><\/p><\/li>

    180.     curl_easy_cleanup(curl);  <\/span><\/p><\/li>

    181.     return<\/span> res;  <\/span><\/span><\/p><\/li>

    182. }  <\/span><\/p><\/li>

    183.   <\/span><\/p><\/li>

    184. \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/span>  <\/span><\/span><\/p><\/li>

    185.   <\/span><\/p><\/li>

    186. void<\/span> CHttpClient::SetDebug(<\/span>bool<\/span> bDebug)  <\/span><\/span><\/p><\/li>

    187. {  <\/span><\/p><\/li>

    188.     m_bDebug = bDebug;  <\/span><\/p><\/li>

    189. }  <\/span><\/p><\/li>

    190. <\/pre><p><\/p>  <\/span><\/p><\/li>

    191. <pre><\/pre>  <\/span><\/p><\/li>

    192. <br>  <\/span><\/p><\/li>

    193. <br>  <\/span><\/p><\/li>

    194. <p><\/p>  <\/span><\/p><\/li>

    195. <br>  <\/span><\/p><\/li><\/ol><\/div>


      <\/p>","orderid":"0","title":" C++\u4f7f\u7528libcurl\u505aHttpClient","smalltitle":"","mid":"0","fname":"\u7f51\u7edc\u7f16\u7a0b","special_id":"0","bak_id":"0","info":"0","hits":"604","pages":"1","comments":"0","posttime":"2014-11-13 11:26:48","list":"1415849208","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"http:\/\/www.cppentry.com\/upload_files\/article\/56\/1_auvrg__.gif","ispic":"1","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"
      \u4f7f\u7528<\/A> libcurl<\/A> HttpClient<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.40","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u5f53\u4f7f\u7528C++\u505aHTTP\u5ba2\u6237\u7aef\u65f6\uff0c\u76ee\u524d\u901a\u7528\u7684\u505a\u6cd5\u5c31\u662f\u4f7f\u7528libcurl\u3002\u5176\u5b98\u65b9\u7f51\u7ad9\u7684\u5730\u5740\u662fhttp:\/\/curl.haxx.se\/\uff0c\u8be5\u7f51\u7ad9\u4e3b\u8981\u63d0\u4f9b\u4e86Curl\u548clibcurl\u3002Curl\u662f\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u7528\u4e8e\u5b8c\u6210FTP,FTPS,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE\u4ee5\u53caLDAP\u7684\u547d\u4ee4\u7684\u8bf7\u6c42\u53ca\u63a5\u6536\u56de\u9988\u3002libcurl\u63d0\u4f9b\u7ed9\u5f00\u53d1\u8005..","lastview":"1484906192","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}