#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"
-----------
//-------------------------------------------------------------------------------------------- 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); }
-----------
//-------------------------------------------------------------------------------------------- static void requestDirectSDK() { // https://learn.microsoft.com/zh-cn/previous-versions/jj950083(v=vs.120) std::string method = "GET"s; std::string url = "https://learn.microsoft.com/zh-cn/previous-versions/jj950083(v=vs.120)"s; std::map<std::string, std::string> headers{ }; // 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); pplx::task<void> &HTTPStreamingAsync = client.request(request).then([=](web::http::http_response &response) { web::http::status_code status = response.status_code(); std::cout << "status: " << status << std::endl; if (status != web::http::status_codes::OK) { return pplx::task_from_result(); } std::map<std::string, std::string> &headers = headers2map(response.headers()); std::string browserResponseType = utility::conversions::to_utf8string(response.headers().content_type()); // std::wstring &result = response.extract_string(true).get(); std::wcout << "size: " << result.size() << std::endl; std::wcout << result.substr(0, 500) << std::endl; return pplx::task_from_result(); }); // // HTTPStreamingAsync.wait(); } //--------------------------------------------------------------------------------------------
-----------
//-------------------------------------------------------------------------------------------- static void requestDirectSDKAsync() { // https://learn.microsoft.com/zh-cn/previous-versions/jj950083(v=vs.120) std::string method = "GET"s; std::string url = "https://learn.microsoft.com/zh-cn/previous-versions/jj950083(v=vs.120)"s; std::map<std::string, std::string> headers{ }; // 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); pplx::task<void> &HTTPStreamingAsync = client.request(request).then([=](web::http::http_response &response) { web::http::status_code status = response.status_code(); std::cout << "status: " << status << std::endl; if (status != web::http::status_codes::OK) { return pplx::task_from_result(); } std::map<std::string, std::string> &headers = headers2map(response.headers()); std::string browserResponseType = utility::conversions::to_utf8string(response.headers().content_type()); // 返回string // return response.extract_string(true).then([](const std::wstring &result) // { // std::wcout << "size: " << result.size() << std::endl; // std::wcout << result.substr(0, 500) << std::endl; // // // }); // 返回 stream concurrency::streams::istream bodyStream = response.body(); concurrency::streams::container_buffer<std::string> inStringBuffer; // we print the first 500 characters of the response to the console. // return bodyStream.read(inStringBuffer, 500).then([inStringBuffer](size_t bytesRead) // { // const std::string &text = inStringBuffer.collection(); // // For demonstration, convert the response text to a wide-character string. // std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf16conv; // std::wostringstream ss; // ss << utf16conv.from_bytes(text.c_str()); // std::wcout << ss.str()<<std::endl; }); // Read a line from the stream into a string. // return bodyStream.read_line(inStringBuffer).then([inStringBuffer](size_t bytesRead) // { // const std::string &line = inStringBuffer.collection(); // std::cout << line << std::endl; // // Perform actions here after reading line into a string... // }); // Read all string return bodyStream.read_to_end(inStringBuffer).then([inStringBuffer](size_t bytesRead) { const std::string &text = inStringBuffer.collection(); std::cout << text << std::endl; // std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf16conv; // std::wstring& str = utf16conv.from_bytes(text.c_str()); // std::wostringstream wss; // wss << str<< std::endl; // std::wcout << wss.str() << std::endl; // }); // }); // // HTTPStreamingAsync.wait(); } //--------------------------------------------------------------------------------------------
------------------------------
void PromiseCommand::OnClick() { // requestDirectSDKAsync(); // // HTTPStreamingAsync.wait(); }标签:std,web,http,string,request,C++,headers,REST,SDK From: https://www.cnblogs.com/gispathfinder/p/17057170.html