HttpProbeImpl.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "RestfulFunc.h"
  2. #include <iostream>
  3. #include "cpprest/uri.h"
  4. #include "cpprest/http_client.h"
  5. #if defined(_MSC_VER)
  6. #include <Windows.h>
  7. #endif //_MSC_VER
  8. using namespace utility; // Common utilities like string conversions
  9. using namespace web; // Common features like URIs.
  10. using namespace web::http; // Common HTTP functionality
  11. using namespace web::http::client; // HTTP client features
  12. using namespace concurrency::streams; // Asynchronous streams
  13. namespace
  14. {
  15. #if defined(_MSC_VER)
  16. static std::wstring s2w(const std::string str)
  17. {
  18. wchar_t* wstr = NULL;
  19. int n = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
  20. if (n > 0) {
  21. wstr = new wchar_t[n + 1];
  22. if (wstr == NULL) {
  23. return std::wstring();
  24. }
  25. std::memset(wstr, 0, (n + 1) * sizeof(wchar_t));
  26. ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wstr[0], n);
  27. std::wstring strr(wstr);
  28. delete wstr;
  29. return strr;
  30. }
  31. return std::wstring();
  32. }
  33. #endif //_MSC_VER
  34. std::string w2s(const std::wstring wstr)
  35. {
  36. #if defined(_MSC_VER)
  37. char* str = NULL;
  38. int n = ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
  39. if (n > 0) {
  40. str = new char[n + 1];
  41. if (str == NULL) {
  42. return std::string();
  43. }
  44. std::memset(str, 0, sizeof(char) * (n + 1));
  45. ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], n, NULL, NULL);
  46. std::string strr(str);
  47. delete str;
  48. return strr;
  49. }
  50. return std::string();
  51. #else
  52. if (wstr.empty()) {
  53. return "";
  54. }
  55. unsigned len = wcstombs(NULL, wstr.c_str(), 0) + 1;
  56. if (NULL == setlocale(LC_ALL, "zh_CN.gbk")) {
  57. }
  58. char* p = new char[len];
  59. wcstombs(p, wstr.c_str(), len);
  60. p[len - 1] = '\0';
  61. std::string str(p);
  62. delete[] p;
  63. return str;
  64. #endif // _MSC_VER
  65. }
  66. }
  67. #if defined(_MSC_VER)
  68. #define STRW(str) s2w(str)
  69. #define WSTR(str) w2s(str)
  70. #else
  71. #define STRW(str) (str)
  72. #define WSTR(str) (str)
  73. #endif //_MSC_VER
  74. int HttpProbe(const std::string& url, std::string& out_msg, uint32_t wait_max_secs)
  75. {
  76. int result(-1);
  77. out_msg.clear();
  78. if (url.empty()) {
  79. out_msg = "invalid param";
  80. result = -3;
  81. } else {
  82. utility::string_t response_content;
  83. utf8string utf8_response_content;
  84. const pplx::task<void> request_task = pplx::task<http_response>([&]() {
  85. http_client_config config;
  86. if (wait_max_secs != (uint32_t)(-1)) {
  87. config.set_timeout(utility::seconds(wait_max_secs));
  88. }
  89. config.set_validate_certificates(false);
  90. http_request request_content(methods::GET);
  91. request_content.headers().add(U("User-Agent"), U("httpclientcpp-agent/1.0"));
  92. http_client client(STRW(url), config);
  93. return client.request(request_content);
  94. }).then([&](http_response response) {
  95. ucout << U(">>>>>> response status: ") << response.status_code() << std::endl;
  96. result = response.status_code();
  97. });
  98. try {
  99. request_task.wait();
  100. } catch (const std::exception& e) {
  101. out_msg = e.what();
  102. std::cout << "exception occurs: " << e.what() << "\n\n";
  103. if (strstr(e.what(), "12002") != nullptr || strstr(e.what(), "Request canceled by user") != nullptr) {
  104. result = -2;
  105. } else if (strstr(e.what(), "URI must contain a hostname") != nullptr) {
  106. result = -3;
  107. } else {
  108. result = -1;
  109. }
  110. }
  111. }
  112. return result;
  113. }