1. 程式人生 > >C++使用libcurl做HttpClient(業務觀摩,用C++封裝過程式代碼,post和get的數據,最好url編碼,否則+會變成空格)good

C++使用libcurl做HttpClient(業務觀摩,用C++封裝過程式代碼,post和get的數據,最好url編碼,否則+會變成空格)good

com 如果 msvc 是否 out tle source rac 無需

當使用C++做HTTP客戶端時,目前通用的做法就是使用libcurl。其官方網站的地址是http://curl.haxx.se/,該網站主要提供了Curl和libcurl。Curl是命令行工具,用於完成FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP的命令的請求及接收回饋。libcurl提供給開發者,用於使用C++跨平臺的開發各種網絡協議的請求及響應。裏面的文檔非常齊全,不過都是英文的。

本文提供最簡單的demo使用libcurl開發HttpClient。主要包括同步的HTTP GET、HTTP POST、HTTPS GET、HTTPS POST。

下載libcurl包,如果使用Linux平臺,建議下載源文件編譯;如果使用Windows平臺,建議下載Win32 - MSVC,下載地址是:http://curl.haxx.se/download.html

[cpp] view plain copy
  1. #ifndef __HTTP_CURL_H__
  2. #define __HTTP_CURL_H__
  3. #include <string>
  4. class CHttpClient
  5. {
  6. public:
  7. CHttpClient(void);
  8. ~CHttpClient(void);
  9. public:
  10. /**
  11. * @brief HTTP POST請求
  12. * @param strUrl 輸入參數,請求的Url地址,如:http://www.baidu.com
  13. * @param strPost 輸入參數,使用如下格式para1=val1¶2=val2&…
  14. * @param strResponse 輸出參數,返回的內容
  15. * @return 返回是否Post成功
  16. */
  17. int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);
  18. /**
  19. * @brief HTTP GET請求
  20. * @param strUrl 輸入參數,請求的Url地址,如:http://www.baidu.com
  21. * @param strResponse 輸出參數,返回的內容
  22. * @return 返回是否Post成功
  23. */
  24. int Get(const std::string & strUrl, std::string & strResponse);
  25. /**
  26. * @brief HTTPS POST請求,無證書版本
  27. * @param strUrl 輸入參數,請求的Url地址,如:https://www.alipay.com
  28. * @param strPost 輸入參數,使用如下格式para1=val1¶2=val2&…
  29. * @param strResponse 輸出參數,返回的內容
  30. * @param pCaPath 輸入參數,為CA證書的路徑.如果輸入為NULL,則不驗證服務器端證書的有效性.
  31. * @return 返回是否Post成功
  32. */
  33. int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL);
  34. /**
  35. * @brief HTTPS GET請求,無證書版本
  36. * @param strUrl 輸入參數,請求的Url地址,如:https://www.alipay.com
  37. * @param strResponse 輸出參數,返回的內容
  38. * @param pCaPath 輸入參數,為CA證書的路徑.如果輸入為NULL,則不驗證服務器端證書的有效性.
  39. * @return 返回是否Post成功
  40. */
  41. int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);
  42. public:
  43. void SetDebug(bool bDebug);
  44. private:
  45. bool m_bDebug;
  46. };
  47. #endif

[cpp] view plain copy
  1. #include "httpclient.h"
  2. #include "curl/curl.h"
  3. #include <string>
  4. CHttpClient::CHttpClient(void) :
  5. m_bDebug(false)
  6. {
  7. }
  8. CHttpClient::~CHttpClient(void)
  9. {
  10. }
  11. static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
  12. {
  13. if(itype == CURLINFO_TEXT)
  14. {
  15. //printf("[TEXT]%s\n", pData);
  16. }
  17. else if(itype == CURLINFO_HEADER_IN)
  18. {
  19. printf("[HEADER_IN]%s\n", pData);
  20. }
  21. else if(itype == CURLINFO_HEADER_OUT)
  22. {
  23. printf("[HEADER_OUT]%s\n", pData);
  24. }
  25. else if(itype == CURLINFO_DATA_IN)
  26. {
  27. printf("[DATA_IN]%s\n", pData);
  28. }
  29. else if(itype == CURLINFO_DATA_OUT)
  30. {
  31. printf("[DATA_OUT]%s\n", pData);
  32. }
  33. return 0;
  34. }
  35. static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
  36. {
  37. std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
  38. if( NULL == str || NULL == buffer )
  39. {
  40. return -1;
  41. }
  42. char* pData = (char*)buffer;
  43. str->append(pData, size * nmemb);
  44. return nmemb;
  45. }
  46. int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)
  47. {
  48. CURLcode res;
  49. CURL* curl = curl_easy_init();
  50. if(NULL == curl)
  51. {
  52. return CURLE_FAILED_INIT;
  53. }
  54. if(m_bDebug)
  55. {
  56. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  57. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  58. }
  59. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  60. curl_easy_setopt(curl, CURLOPT_POST, 1);
  61. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
  62. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  63. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  64. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  65. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  66. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
  67. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
  68. res = curl_easy_perform(curl);
  69. curl_easy_cleanup(curl);
  70. return res;
  71. }
  72. int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
  73. {
  74. CURLcode res;
  75. CURL* curl = curl_easy_init();
  76. if(NULL == curl)
  77. {
  78. return CURLE_FAILED_INIT;
  79. }
  80. if(m_bDebug)
  81. {
  82. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  83. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  84. }
  85. <pre name="code" class="cpp"> curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  86. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  87. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  88. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  89. /**
  90. * 當多個線程都使用超時處理的時候,同時主線程中有sleep或是wait等操作。
  91. * 如果不設置這個選項,libcurl將會發信號打斷這個wait從而導致程序退出。
  92. */
  93. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  94. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
  95. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
  96. res = curl_easy_perform(curl);
  97. curl_easy_cleanup(curl);
  98. return res;
  99. }
  100. int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)
  101. {
  102. CURLcode res;
  103. CURL* curl = curl_easy_init();
  104. if(NULL == curl)
  105. {
  106. return CURLE_FAILED_INIT;
  107. }
  108. if(m_bDebug)
  109. {
  110. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  111. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  112. }
  113. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  114. curl_easy_setopt(curl, CURLOPT_POST, 1);
  115. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
  116. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  117. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  118. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  119. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  120. if(NULL == pCaPath)
  121. {
  122. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  123. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  124. }
  125. else
  126. {
  127. //缺省情況就是PEM,所以無需設置,另外支持DER
  128. //curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
  129. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
  130. curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
  131. }
  132. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
  133. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
  134. res = curl_easy_perform(curl);
  135. curl_easy_cleanup(curl);
  136. return res;
  137. }
  138. int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)
  139. {
  140. CURLcode res;
  141. CURL* curl = curl_easy_init();
  142. if(NULL == curl)
  143. {
  144. return CURLE_FAILED_INIT;
  145. }
  146. if(m_bDebug)
  147. {
  148. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  149. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  150. }
  151. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  152. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  153. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  154. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  155. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  156. if(NULL == pCaPath)
  157. {
  158. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  159. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  160. }
  161. else
  162. {
  163. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
  164. curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
  165. }
  166. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
  167. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
  168. res = curl_easy_perform(curl);
  169. curl_easy_cleanup(curl);
  170. return res;
  171. }
  172. ///////////////////////////////////////////////////////////////////////////////////////////////
  173. void CHttpClient::SetDebug(bool bDebug)
  174. {
  175. m_bDebug = bDebug;
  176. }
http://blog.csdn.net/huyiyang2010/article/details/7664201 當POST的內容為para=val,其中val包含‘+‘的時候會把‘+‘替換為‘ ‘,例如表單中有一項隱藏驗證內容
[html] view plain copy
  1. <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNTc4MTM4MzMzZGQF+FxsPex77Sht1ZxPy5wLSXk1lpdzRqG9qS69Ybnkmg==" />

服務器收到的__VIEWSTATE值為
[plain] view plain copy
  1. <br>/wEPDwUJNTc4MTM4MzMzZGQF FxsPex77Sht1ZxPy5wLSXk1lpdzRqG9qS69Ybnkmg==<br>

不知道是不是本來就有這個BUG
Re: xiejienet 2013-12-23 01:03發表 [回復]
回復xingtianduiyue:post和get的數據,最好url編碼

C++使用libcurl做HttpClient(業務觀摩,用C++封裝過程式代碼,post和get的數據,最好url編碼,否則+會變成空格)good