CurlHttp.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "CurlHttp.h"
  2. #include <regex>
  3. #include "fmtlog.h"
  4. /* ==================================================================================
  5. * *********************************** 全局变量 *************************************
  6. * ================================================================================== */
  7. /* ==================================================================================
  8. * *********************************** 全局函数 *************************************
  9. * ================================================================================== */
  10. /**
  11. * @brief 写入回调函数
  12. *
  13. * @param contents 接收到的内容
  14. * @param size 内容大小,单位size_t
  15. * @param nmemb size_t的单位
  16. * @param userp 用户传入的接收区指针
  17. * @return size_t
  18. */
  19. static size_t WriteStringCallback(void* contents, size_t size, size_t nmemb, std::string* userStr)
  20. {
  21. size_t newLength = size * nmemb;
  22. size_t oldLength = userStr->size();
  23. try
  24. {
  25. userStr->resize(oldLength + newLength);
  26. }
  27. catch(std::bad_alloc &e)
  28. {
  29. FMTLOG_ERROR("{}", e.what());
  30. return 0;
  31. }
  32. std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
  33. return size * nmemb;
  34. }
  35. /* ==================================================================================
  36. * *********************************** 成员函数 *************************************
  37. * ================================================================================== */
  38. CurlHttp::CurlHttp()
  39. {
  40. }
  41. CurlHttp::~CurlHttp()
  42. {
  43. }
  44. /**
  45. * @brief 获取信息
  46. *
  47. * @param url 网址
  48. * @param response 返回的数据
  49. * @return true
  50. * @return false
  51. */
  52. bool CurlHttp::Get(const std::string& url, std::string& response)
  53. {
  54. CURL *curl;
  55. curl = curl_easy_init();
  56. if(curl == nullptr)
  57. {
  58. FMTLOG_ERROR("curl_easy_init() failed");
  59. return false;
  60. }
  61. /* 设置用户名密码,可能有的话 */
  62. return true;
  63. }
  64. /**
  65. * @brief 发送信息,不携带http头
  66. *
  67. * @param url
  68. * @param postData
  69. * @param response
  70. * @return true
  71. * @return false
  72. */
  73. bool CurlHttp::Post(const std::string& url, const std::string& postData, std::string& response)
  74. {
  75. CURL *curl;
  76. CURLcode res;
  77. curl = curl_easy_init();
  78. if(curl == nullptr)
  79. {
  80. FMTLOG_ERROR("curl_easy_init() failed");
  81. return false;
  82. }
  83. /* 设置用户名密码,可能有的话 */
  84. /* 设置动作功能和网址 */
  85. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  86. curl_easy_setopt(curl, CURLOPT_URL, "http://172.16.36.80:30000/vos/external/broadcastToken");
  87. /* 设置重定向,遇到3xx返回值时自动重定向 */
  88. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  89. /* 设置https协议 */
  90. curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
  91. /* 设置http头 */
  92. struct curl_slist *headers = NULL;
  93. headers = curl_slist_append(headers, "User-Agent: Apifox/1.0.0 (https://apifox.com)");
  94. headers = curl_slist_append(headers, "Content-Type: application/json");
  95. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  96. /* 设置包体 */
  97. const char *data = R"({"appSecret":"8zR9ug4WbKCtEgaQ08myRm5HqKIMwP83Ra24t921tO/mU0cTIFdm/t0C9Jxrd53x","appKey":"rili4l26"})";
  98. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  99. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(data));
  100. /* 设置回调函数 */
  101. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteStringCallback);
  102. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  103. /* 设置连接超时和接收数据超时 */
  104. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
  105. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  106. /* 设置不发送任何信号,对于多线程有用 */
  107. // curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  108. /* 发送请求 */
  109. res = curl_easy_perform(curl);
  110. bool result = true;
  111. if(res != CURLE_OK)
  112. {
  113. FMTLOG_ERROR("curl_easy_perform() failed: {}", curl_easy_strerror(res));
  114. result = false;
  115. }
  116. /* 清理curl */
  117. curl_easy_cleanup(curl);
  118. return result;
  119. }
  120. /**
  121. * @brief 发送带有Http头和包体的信息
  122. *
  123. * @param url 网址
  124. * @param vecHeader http头,vector<string>格式,每个元素为一行,
  125. * 如 User-Agent: Apifox/1.0.0 (https://apifox.com)
  126. * @param postData 发送的包体
  127. * @param response 返回的数据
  128. * @return true
  129. * @return false
  130. */
  131. bool CurlHttp::PostWithBody(const std::string& url,const std::vector<std::string>& vecHeader, const std::string& postData, std::string& response)
  132. {
  133. CURL *curl;
  134. CURLcode res;
  135. curl = curl_easy_init();
  136. if(curl == nullptr)
  137. {
  138. FMTLOG_ERROR("curl_easy_init() failed");
  139. return false;
  140. }
  141. /* 设置动作功能和网址 */
  142. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  143. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  144. /* 设置重定向,遇到3xx返回值时自动重定向 */
  145. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  146. /* 设置https协议 */
  147. curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
  148. /* 设置http头 */
  149. if(!vecHeader.empty())
  150. {
  151. struct curl_slist *headers = NULL;
  152. for(auto &header : vecHeader)
  153. {
  154. headers = curl_slist_append(headers, header.c_str());
  155. }
  156. }
  157. /* 设置包体 */
  158. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
  159. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
  160. /* 设置回调函数 */
  161. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteStringCallback);
  162. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  163. /* 设置连接超时和接收数据超时 */
  164. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
  165. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  166. /* 设置不发送任何信号,对于多线程有用 */
  167. // curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  168. /* 发送请求 */
  169. res = curl_easy_perform(curl);
  170. bool result = true;
  171. if(res != CURLE_OK)
  172. {
  173. FMTLOG_ERROR("curl_easy_perform() failed: {}", curl_easy_strerror(res));
  174. result = false;
  175. }
  176. /* 清理curl */
  177. curl_easy_cleanup(curl);
  178. return result;
  179. }