多数据源中间件标准版1.0

CHttpUtil.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "stdafx.h"
  2. #include "CHttpUtil.h"
  3. SINGLETON_IMPLEMENT(CHttpUtil)
  4. CHttpUtil::CHttpUtil()
  5. {
  6. // global init
  7. curl_global_init(CURL_GLOBAL_ALL);
  8. }
  9. CHttpUtil::~CHttpUtil()
  10. {
  11. // global release
  12. curl_global_cleanup();
  13. }
  14. // reply of the requery
  15. size_t CHttpUtil::req_reply(void *ptr, size_t size, size_t nmemb, void *stream)
  16. {
  17. string *str = (string*)stream;
  18. (*str).append((char*)ptr, size*nmemb);
  19. return size * nmemb;
  20. }
  21. // http GET
  22. CURLcode CHttpUtil::curl_get_req(const std::string &url, std::string &response)
  23. {
  24. // init curl
  25. CURL *curl = curl_easy_init();
  26. // res code
  27. CURLcode res;
  28. if (curl)
  29. {
  30. // set params
  31. curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url
  32. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
  33. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false
  34. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  35. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  36. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
  37. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
  38. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  39. curl_easy_setopt(curl, CURLOPT_HEADER, 0); // 是否返回头信息 0:不返回 1:返回
  40. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); // set transport and time out time
  41. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
  42. // start req
  43. res = curl_easy_perform(curl);
  44. }
  45. // release curl
  46. curl_easy_cleanup(curl);
  47. response = UTF8ToGBK(response);
  48. return res;
  49. }
  50. // http POST
  51. CURLcode CHttpUtil::curl_post_req(const string &url, const string &postParams, string &response)
  52. {
  53. // init curl
  54. CURL *curl = curl_easy_init();
  55. // res code
  56. CURLcode res;
  57. if (curl)
  58. {
  59. // set params
  60. curl_easy_setopt(curl, CURLOPT_POST, 1); // post req
  61. curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // url
  62. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str()); // params
  63. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https
  64. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false
  65. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  66. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  67. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
  68. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
  69. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  70. curl_easy_setopt(curl, CURLOPT_HEADER, 1);
  71. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
  72. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
  73. // start req
  74. res = curl_easy_perform(curl);
  75. }
  76. // release curl
  77. curl_easy_cleanup(curl);
  78. return res;
  79. }
  80. string CHttpUtil::UTF8ToGBK(const std::string& strUTF8)
  81. {
  82. int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
  83. WCHAR* wszGBK = new WCHAR[len + 1];
  84. memset(wszGBK, 0, len * 2 + 2);
  85. MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, wszGBK, len);
  86. len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
  87. char *szGBK = new char[len + 1];
  88. memset(szGBK, 0, len + 1);
  89. WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
  90. std::string strTemp(szGBK);
  91. delete[]szGBK;
  92. delete[]wszGBK;
  93. return strTemp;
  94. }
  95. string CHttpUtil::GBKToUTF8(const std::string& strGBK)
  96. {
  97. string strOutUTF8 = "";
  98. WCHAR * str1;
  99. int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
  100. str1 = new WCHAR[n];
  101. MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
  102. n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
  103. char * str2 = new char[n];
  104. WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
  105. strOutUTF8 = str2;
  106. delete[]str1;
  107. str1 = NULL;
  108. delete[]str2;
  109. str2 = NULL;
  110. return strOutUTF8;
  111. }