如何使用cpprestsdk处理块响应?如何请求下一块?这里是否有所需的功能?
下面是我们执行http请求的方式:
web::http::http_request request(web::http::methods::GET);
request.headers().add(LR"(User-Agent)", LR"(ExchangeServicesClient/15.00.0847.030)");
request.headers().add(LR"(Accept)", LR"(text/xml)");
request.set_body(L"request body", L"text/xml");
web::http::client::http_client_config clientConfig;
clientConfig.set_credentials(web::credentials(L"username", L"pass"));
clientConfig.set_validate_certificates(true);
web::http::client::http_client client(L"serviceurl", clientConfig);
auto bodyTask = client.request(request)
.then([](web::http::http_response response) {
auto str = response.extract_string().get();
return str;
});
auto body = bodyTask.get();
如果我只是天真地尝试在这个请求之后执行另一个请求,那么我得到了一个错误:
WinHttpSendRequest: 5023:组或资源未处于执行请求操作的正确状态。
发布于 2019-08-14 21:38:33
为了读取大量接收到的数据,需要从服务器响应中获取输入流。
concurrency::streams::istream bodyStream = response.body();
然后从该流中连续读取,直到找到给定的字符或读取指定字节数为止。
pplx::task<void> repeat(Concurrency::streams::istream bodyStream)
{
Concurrency::streams::container_buffer<std::string> buffer;
return pplx::create_task([=] {
auto t = bodyStream.read_to_delim(buffer, '\n').get();
std::cout << buffer.collection() << std::endl;
return t;
}).then([=](int /*bytesRead*/) {
if (bodyStream.is_eof()) {
return pplx::create_task([]{});
}
return repeat(bodyStream);
});
}
下面是完整的示例:stream
https://stackoverflow.com/questions/42923074
复制相似问题