CPPrestsdk:处理分块响应
cpprestsdk: handle chunked response
我应该如何使用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:组或资源未处于正确的状态,无法执行 请求的操作。
为了以块的形式读取接收到的数据,需要从服务器响应中获取输入流
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);
});
}
以下是完整示例:https://github.com/cristeab/oanda_stream
相关文章:
- 警告处理为错误这里有什么问题
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用流处理接收到的数据
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 基于多个条件处理地图中的所有元素
- 如何用数字处理log(0)
- SSL上的`curl_easy_send`和`curl_asy_recv`:如何处理`CURLE_AGAIN`
- 如何使用从处理程序调度的最终回调将响应异步返回给调用方on_read?
- 如何在 rxcpp 中处理请求/响应流
- CPPrestsdk:处理分块响应
- 处理HTTP POST请求/响应
- 处理Windows消息,以便我的应用程序正确响应
- 如何保持Qt小部件响应键盘和鼠标,同时处理Windows事件
- C++ 将数据发送到 PHP 并处理响应
- 如何处理大容量模型更新场景中无响应的UI
- 如何在c++中异步处理本地socket上的https请求响应
- 将我的服务器响应char数组转换为wchar_t数组是处理客户端收到的服务器消息的正确方法吗?