123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #include "RestfulFunc.h"
- #include <iostream>
- #include "cpprest/uri.h"
- #include "cpprest/http_client.h"
- 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
- 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_client client(url, config);
- return client.request(methods::GET);
- }).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;
- }
|