http_client.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "http_client.h"
  2. #define HTTP_TIMEOUT_MSECS 120000
  3. string HttpClient::proxyIp;
  4. string HttpClient::proxyPort;
  5. string HttpClient::proxyUserName;
  6. string HttpClient::proxyPassword;
  7. HttpClient::HttpClient()
  8. {
  9. CURLcode init_ret = curl_global_init(CURL_GLOBAL_ALL);
  10. curl = curl_easy_init();
  11. responseEntity.clear();
  12. }
  13. HttpClient::~HttpClient() {
  14. curl_easy_cleanup(curl);
  15. curl_global_cleanup();
  16. }
  17. void HttpClient::setProxy(const string &proxyIp,
  18. const string &proxyPort,
  19. const string &proxyUserName,
  20. const string &proxyPassword) {
  21. HttpClient::proxyIp = proxyIp;
  22. HttpClient::proxyPort = proxyPort;
  23. HttpClient::proxyUserName = proxyUserName;
  24. HttpClient::proxyPassword = proxyPassword;
  25. }
  26. size_t HttpClient::write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
  27. int length = size * nmemb;
  28. HttpClient *client = (HttpClient *)stream;
  29. client->responseEntity = string((char *)ptr, length);
  30. return length;
  31. }
  32. string HttpClient::sendSyncRequest(const string &url,
  33. const string &requestEntity,
  34. const HeaderMap &headers) {
  35. if(curl == NULL) {
  36. DebugLog("%s | %s", url.c_str(), "curl easy init error");
  37. return string();
  38. }
  39. struct curl_slist * headerlist = NULL;
  40. string headitem;
  41. for (HeaderMap::const_iterator iter = headers.begin(); iter != headers.end(); ++iter) {
  42. headitem = iter->first;
  43. headitem += " : ";
  44. headitem += iter->second;
  45. headerlist = curl_slist_append(headerlist, headitem.c_str());
  46. headitem.clear();
  47. }
  48. //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);//调试信息打开
  49. //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 0L);
  50. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  51. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  52. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  53. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  54. curl_easy_setopt(curl, CURLOPT_POST, 1);
  55. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestEntity.c_str());
  56. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  57. if (!proxyIp.empty() && !proxyPort.empty()) {
  58. string proxyStr = proxyIp;
  59. proxyStr += ":";
  60. proxyStr += proxyPort;
  61. curl_easy_setopt(curl, CURLOPT_PROXY, proxyStr.c_str());
  62. if (!proxyUserName.empty() && !proxyPassword.empty()) {
  63. string proxyUserPwd = proxyUserName;
  64. proxyUserPwd += ":";
  65. proxyUserPwd += proxyPassword;
  66. curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxyUserPwd.c_str());
  67. }
  68. }
  69. DebugLog("send:%s", requestEntity.c_str());
  70. responseEntity.clear();
  71. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  72. curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
  73. curl_easy_perform(curl);
  74. curl_slist_free_all(headerlist);
  75. DebugLog("resv:%s", responseEntity.c_str());
  76. return responseEntity;
  77. }
  78. string HttpClient::sendSyncRequest(const string &url,
  79. const ParamsMap &paramsMap,
  80. const HeaderMap &headers) {
  81. string requestEntity;
  82. string item;
  83. for (ParamsMap::const_iterator iter = paramsMap.begin(); iter != paramsMap.end(); ++iter) {
  84. const char *key = iter->first.c_str();
  85. char *encodedKey = curl_easy_escape(curl, key, strlen(key));
  86. if (encodedKey) {
  87. item = encodedKey;
  88. }
  89. item += "=";
  90. const char *value = iter->second.c_str();
  91. char *encodedValue = curl_easy_escape(curl, value, strlen(value));
  92. if (encodedValue) {
  93. item += encodedValue;
  94. }
  95. if (!requestEntity.empty()) {
  96. requestEntity.push_back('&');
  97. }
  98. requestEntity.append(item);
  99. item.clear();
  100. if (encodedKey) {
  101. curl_free(encodedKey);
  102. }
  103. if (encodedValue) {
  104. curl_free(encodedValue);
  105. }
  106. }
  107. string responseStr = sendSyncRequest(url, requestEntity, headers);
  108. return responseStr;
  109. }