123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #include "RestfulFunc.h"
- #include <iostream>
- #include "cpprest/uri.h"
- #include "cpprest/http_client.h"
- #if defined(_MSC_VER)
- #include <Windows.h>
- #endif //_MSC_VER
- using namespace utility; // Common utilities like string conversions
- using namespace web; // Common features like URIs.
- using namespace web::http; // Common HTTP functionality
- using namespace web::http::client; // HTTP client features
- using namespace concurrency::streams; // Asynchronous streams
- namespace
- {
- #if defined(_MSC_VER)
- static std::wstring s2w(const std::string str)
- {
- wchar_t* wstr = NULL;
- int n = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
- if (n > 0) {
- wstr = new wchar_t[n + 1];
- if (wstr == NULL) {
- return std::wstring();
- }
- std::memset(wstr, 0, (n + 1) * sizeof(wchar_t));
- ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wstr[0], n);
- std::wstring strr(wstr);
- delete wstr;
- return strr;
- }
- return std::wstring();
- }
- #endif //_MSC_VER
- std::string w2s(const std::wstring wstr)
- {
- #if defined(_MSC_VER)
- char* str = NULL;
- int n = ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
- if (n > 0) {
- str = new char[n + 1];
- if (str == NULL) {
- return std::string();
- }
- std::memset(str, 0, sizeof(char) * (n + 1));
- ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], n, NULL, NULL);
- std::string strr(str);
- delete str;
- return strr;
- }
- return std::string();
- #else
- if (wstr.empty()) {
- return "";
- }
- unsigned len = wcstombs(NULL, wstr.c_str(), 0) + 1;
- if (NULL == setlocale(LC_ALL, "zh_CN.gbk")) {
- }
- char* p = new char[len];
- wcstombs(p, wstr.c_str(), len);
- p[len - 1] = '\0';
- std::string str(p);
- delete[] p;
- return str;
- #endif // _MSC_VER
- }
- }
- #if defined(_MSC_VER)
- #define STRW(str) s2w(str)
- #define WSTR(str) w2s(str)
- #else
- #define STRW(str) (str)
- #define WSTR(str) (str)
- #endif //_MSC_VER
- int HttpProbe(const std::string& url, std::string& out_msg, uint32_t wait_max_secs)
- {
- int result(-1);
- out_msg.clear();
- if (url.empty()) {
- out_msg = "invalid param";
- result = -3;
- } else {
- utility::string_t response_content;
- utf8string utf8_response_content;
- const pplx::task<void> request_task = pplx::task<http_response>([&]() {
- http_client_config config;
- if (wait_max_secs != (uint32_t)(-1)) {
- config.set_timeout(utility::seconds(wait_max_secs));
- }
- config.set_validate_certificates(false);
- http_request request_content(methods::GET);
- request_content.headers().add(U("User-Agent"), U("httpclientcpp-agent/1.0"));
- http_client client(STRW(url), config);
- return client.request(request_content);
- }).then([&](http_response response) {
- ucout << U(">>>>>> response status: ") << response.status_code() << std::endl;
- result = response.status_code();
- });
- try {
- request_task.wait();
- } catch (const std::exception& e) {
- out_msg = e.what();
- std::cout << "exception occurs: " << e.what() << "\n\n";
- if (strstr(e.what(), "12002") != nullptr || strstr(e.what(), "Request canceled by user") != nullptr) {
- result = -2;
- } else if (strstr(e.what(), "URI must contain a hostname") != nullptr) {
- result = -3;
- } else {
- result = -1;
- }
- }
- }
- return result;
- }
|