promise-cpp应用--02与REST SDK配合应用
--------------------------
#include <iostream> #include <future> #include <string> #include <sstream> #include <stdexcept> #include <functional> #include <locale> #include <codecvt> // #include "pplx/pplxtasks.h" // #include <ppltasks.h> #include "cpprest/uri.h" #include "cpprest/uri_builder.h" #include "cpprest/http_client.h" #include "cpprest/json.h" // #ifndef PROMISE_HEADONLY #define PROMISE_HEADONLY #endif #include "promise-cpp/promise.hpp"
-----------------------
//-------------------------------------------------------------------------------------------- static std::map<std::string, std::string> &headers2map(web::http::http_headers &headers) { std::map<std::string, std::string> map; for (auto &header : headers) { // utility::conversions::to_string_t(header.first); map.emplace(utility::conversions::to_utf8string(header.first), utility::conversions::to_utf8string(header.second)); } return std::move(map); }
-----------------------
//-------------------------------------------------------------------------------------------- promise::Promise &loadWithXhr(std::string &url, std::string &responseType, std::string &method, std::string &data, const std::multimap<std::string, std::string> &headers, promise::Defer &deferred) { // web::json::value &headersJson = multimap2json(headers); std::transform(method.begin(), method.end(), method.begin(), towupper); // towupper towlower web::http::method methodPara = utility::conversions::to_string_t(method); // web::uri uri(utility::conversions::to_string_t(url)); web::http::client::http_client client(uri); // web::http::http_request request; request.set_method(methodPara); request.set_request_uri(L""); web::http::http_headers &req_headers = request.headers(); // request.headers().add(web::http::header_names::accept, L"application/json"); for (auto &item : headers) { req_headers.add(utility::conversions::to_string_t(item.first), utility::conversions::to_string_t(item.second)); } if (methodPara != web::http::methods::GET && methodPara != web::http::methods::HEAD) { if (data != "") { // web::json::value jvalue = web::json::value::string(utility::conversions::to_string_t(data)); // request.set_body(jvalue); //responseTask = client.request(methodPara, L"", jvalue); request.set_body(utility::conversions::to_string_t(data)); } } // pplx::task<web::http::http_response> responseTask = client.request(request); // client.request(methodPara); client.request(request) .then([=](web::http::http_response &response) { web::http::status_code status = response.status_code(); std::map<std::string, std::string> &headers = headers2map(response.headers()); std::string browserResponseType = utility::conversions::to_utf8string(response.headers().content_type()); // if ((status < 200 || status >= 300)) // && !(localFile && xhr.status == 0) { std::exception ex("request error!!!"); deferred.reject(ex); return; } std::string &value = response.extract_utf8string(true).get(); // std::cout << value << std::endl; deferred.resolve(headers, value); // }); return deferred.getPromise(); }
-----------------------
//-------------------------------------------------------------------------------------------- static promise::Promise HttpRequestPromise() { std::string url = "https://learn.microsoft.com/zh-cn/previous-versions/jj950083(v=vs.120)"s; url = "https://www.cnblogs.com/gispathfinder/ajax/TopLists.aspx"s; url = "https://www.cnblogs.com/gispathfinder/ajax/TopLists-error.aspx"s; std::string responseType = ""s; std::string method = "GET"s; std::string data = ""s; std::multimap<std::string, std::string> headers{}; // return promise::newPromise([&](promise::Defer &deferred) { loadWithXhr(url, responseType, method, data, headers, deferred); // }) .then([](std::map<std::string, std::string> &headers, std::string &responseStr) // { // std::cout << responseStr << std::endl; }) .fail([](std::exception &ex) { // std::cout << "exception: " << ex.what() << std::endl; }); // .then([](std::map<std::string, std::string> &headers, std::string &responseStr) // // { // // std::cout << responseStr << std::endl; // }, // // [](std::exception &ex) { // // std::cout << ex.what() << std::endl; // }); }
-----------------------
//-------------------------------------------------------------------------------------------- void PromiseCommand::OnClick() { // // requestDirectSDKAsync(); // // HTTPStreamingAsync.wait(); // testPromise(); HttpRequestPromise(); }
-----------------------