英文:
Sending data from client to server response_tak = client.request(req) I want to add timeout functionality if response doesn't come in particular time
问题
utility::string_t url = U("http://localhost:8080/api/v1/post_info");
web::uri uri1(url);
web::http::client::http_client client(uri1);
web::http::http_request request;
pplx::task<web::http::http_response> response_task;
web::http::http_response response;
request.set_method(web::http::methods::POST);
request.set_body(jsondata);
response_task = client.request(request);
response = response_task.get();
If response
doesn't come from client.request(request);
or if it is taking too much time, then my .exe will just wait continuously? So what should I do?
web::http::client::http_client::http_client(const uri &base_uri, const http_client_config &client_config);
在cpprestsdk库中有这个函数,但关于http_client_config
类的utility::seconds web::http::client::http_client_config::timeout() const
函数没有提供太多信息。
英文:
utility::string_t url = U("http://localhost:8080/api/v1/post_info");
web::uri uri1( url);
web::http::client::http_client client( uri1);
web::http::http_request request;
pplx::task<web::http::http_response> response_task;
web::http::http_response response;
request.set_method( web::http::methods::POST);
request.set_body(jsondata);
response_task = client.request(request);
response = response_task.get();
If response doesn't come from client.request(request);
or if it is taking too much time then My .exe will just wait continiously ? So what should I do ?
web::http::client::http_client::http_client( const uri &base_uri, const http_client_config &client_config );
There is this function in cpprestsdk library but nothing much given about this http_client_config
class's
utility::seconds web::http::client::http_client_config::timeout()const
function.
答案1
得分: 1
你可以通过创建http_client_config
对象并使用void web::http::client::http_client_config::set_timeout ( const T & timeout )
来为所有请求设置超时时间,文档。然后,你需要将配置类作为第二个参数传递给构造函数,使用你提到的方法web::http::client::http_client::http_client( const uri &base_uri, const http_client_config &client_config );
类pplx::task<web::http::http_response>
是异步的,如果直接调用.Get()
会被阻塞。你应该要么检查响应是否已经存在:
bool done = resp.is_done();
或者使用回调函数:
resp.then([=](pplx::task<web::http::http_response> task)
{
web::http::http_response response = task.get();
...
});
如果
is_done()
返回false,调用get()
将会有阻塞线程的风险,这违背了首次使用异步API的初衷(它会阻止GUI刷新和服务器扩展)。对于这种情况,我们需要采用不同的方法:将处理函数附加到任务上,一旦任务完成就会被调用。我们使用then()
函数来实现这个目标。
英文:
You can set the timeout for all requests by creating the http_client_config object and using void web::http::client::http_client_config::set_timeout ( const T & timeout )
, docu. Then you need to give the config class into the constructor as the second parameter, using the method you mentioned web::http::client::http_client::http_client( const uri &base_uri, const http_client_config &client_config );
The class pplx::task<web::http::http_response>
is async, if you call .Get()
directly it will be blocked. You should either check if the response is already there with
bool done = resp.is_done();
or use a callback function
resp.then([=](pplx::task<web::http::http_response> task)
{
web::http::http_response response = task.get();
...
});
> If is_done() returns false, calling get() will risk blocking the
> thread, which defeats the purpose of using an asynchronous API in the
> first place (it will keep GUIs from refreshing and servers from
> scaling). For this situation, we need to take a different approach:
> attach a handler function to the task, which will be invoked once the
> task completes. We do this using the then() function
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论