CHttpClient.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "../pch/pch.h"
  2. #include "CHttpClient.h"
  3. #include <curl/curl.h>
  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, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
  67. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
  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. 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, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
  95. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
  96. res = curl_easy_perform(curl);
  97. curl_easy_cleanup(curl);
  98. return res;
  99. }
  100. /*
  101. *返回值说明 https://blog.csdn.net/u011857683/article/details/53069268
  102. **/
  103. int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)
  104. {
  105. CURLcode res;
  106. CURL* curl = curl_easy_init();
  107. if (NULL == curl)
  108. {
  109. return CURLE_FAILED_INIT;
  110. }
  111. if (m_bDebug)
  112. {
  113. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  114. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  115. }
  116. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  117. curl_easy_setopt(curl, CURLOPT_POST, 1);
  118. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
  119. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  120. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  121. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  122. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  123. //curl_easy_setopt(curl, CURLOPT_COOKIE, "lwm_gray_tag=rc");;
  124. if (NULL == pCaPath)
  125. {
  126. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  127. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  128. }
  129. else
  130. {
  131. //缺省情况就是PEM,所以无需设置,另外支持DER
  132. //curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
  133. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
  134. curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
  135. }
  136. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
  137. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
  138. res = curl_easy_perform(curl);
  139. curl_easy_cleanup(curl);
  140. return res;
  141. }
  142. int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)
  143. {
  144. CURLcode res;
  145. CURL* curl = curl_easy_init();
  146. if (NULL == curl)
  147. {
  148. return CURLE_FAILED_INIT;
  149. }
  150. if (m_bDebug)
  151. {
  152. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  153. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
  154. }
  155. curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
  156. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  157. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
  158. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
  159. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  160. if (NULL == pCaPath)
  161. {
  162. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  163. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  164. }
  165. else
  166. {
  167. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
  168. curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
  169. }
  170. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
  171. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
  172. res = curl_easy_perform(curl);
  173. curl_easy_cleanup(curl);
  174. return res;
  175. }
  176. ///////////////////////////////////////////////////////////////////////////////////////////////
  177. void CHttpClient::SetDebug(bool bDebug)
  178. {
  179. m_bDebug = bDebug;
  180. }