微軟開源跨平台 HTTP 庫,基于 C++11,集成 PPL (Parallel Patterns Library) 與 Asio,支持異步流、WebSocket、JSON 處理。
http_client client(U("https://www.bing.com/"));
uri_builder builder(U("/search"));
builder.append_query(U("q"), U("Casablanca CodePlex"));
http_response resp = client.request(methods::GET, builder.to_string()).get();
auto body = resp.extract_json().get(); // 解析 JSON
// 或 resp.body().read_to_end(stream).get() 存文件
http_client:發起請求,可配置超時、代理、憑證。uri_builder:拼接路徑、查詢參數。json::value:構造/解析 JSON,支持 number, string, object, array。http_response:狀態碼、頭部、體(流/JSON)。json::value obj;
obj[U("Count")] = json::value::number(6);
obj[U("Version")] = json::value::string(U("1.0"));
client.request(methods::POST, U("/api"), obj.serialize(), U("application/json"));
auto task = fstream::open_ostream(U("out.html"))
.then([=](ostream out) {
*fileStream = out;
return client.request(methods::GET, builder.to_string());
})
.then([=](http_response resp) { return resp.body().read_to_end(fileStream->streambuf()); })
.then([=](size_t) { return fileStream->close(); });
task.wait();
http_client_config cfg;
cfg.set_timeout(chrono::seconds(90));
http_client client(URL, cfg);
REST SDK 封裝完善,異步模型強大,適合現代 C++ 開發 REST 客戶端。官方文檔與範例豐富,值得深入學習。