Windows 安裝方法:
CMake 1.32+,生成過程會將 vcpkg 下載好,配置到系統環境變量,然后用 vcpkg 安裝依賴庫(github 上有列出需要的依賴庫)。
Github 上的示例不全面,一開始不知道如何序列化輸出 json,經過一番研究和谷歌,這里給一個示例:
#include <cpprest/http_client.h> #include <cpprest/filestream.h> //#include <boost/regex.hpp> #include <string> //#pragma comment(lib, "C:/Program Files/boost_1_72_0/bin.v2/libs/regex/build/msvc-14.2/release/address-model-64/link-static/threading-multi/libboost_regex-vc142-mt-x64-1_72.lib") #pragma comment(lib, "C:/cpprestsdk/Build_x64/Binaries/Debug/cpprest142_2_10d.lib") using namespace utility; using namespace web; using namespace web::http; using namespace web::http::client; using namespace concurrency::streams; int main() { http_client raw_client(U("https://message.bilibili.com/api/tooltip/query.list.do")); http_request request(methods::GET); request.headers().add(header_names::accept, L"application/json"); request.headers().add(header_names::user_agent, L"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"); request.headers().add(header_names::accept_encoding, L"gzip, deflate, br"); pplx::task<json::value> requestTask = raw_client.request(request).then([](http_response response) { return response.extract_json(); }); std::wcout << requestTask.get().serialize() << std::endl; return 0; }
當然也可以不用 lambda 風格,直接:
std::wcout << raw_client.request(request) .get().extract_json().get().serialize() << std::endl;