gpt4 book ai didi

c++ - cpprestsdk : handle chunked response

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:44:01 35 4
gpt4 key购买 nike

我应该如何使用 cpprestsdk 处理分块响应?如何请求下一个 block ?那里是否有所需的功能?

这是我们执行 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: The group or resource is not in the correct state to perform the requested operation.

最佳答案

为了以 block 的形式读取接收到的数据,需要从服务器响应中获取输入流

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++ - cpprestsdk : handle chunked response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42923074/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com