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

huangapple go评论79阅读模式
英文:

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(&quot;http://localhost:8080/api/v1/post_info&quot;);
 web::uri uri1( url);
 web::http::client::http_client client( uri1);
 web::http::http_request request;
 pplx::task&lt;web::http::http_response&gt; 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 &amp;base_uri, const http_client_config &amp;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 &amp; timeout )来为所有请求设置超时时间,文档。然后,你需要将配置类作为第二个参数传递给构造函数,使用你提到的方法web::http::client::http_client::http_client( const uri &amp;base_uri, const http_client_config &amp;client_config );

pplx::task&lt;web::http::http_response&gt;是异步的,如果直接调用.Get()会被阻塞。你应该要么检查响应是否已经存在:

bool done = resp.is_done();

或者使用回调函数:

resp.then([=](pplx::task&lt;web::http::http_response&gt; 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 &amp; 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 &amp;base_uri, const http_client_config &amp;client_config );

The class pplx::task&lt;web::http::http_response&gt; 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&lt;web::http::http_response&gt; 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

more information

huangapple
  • 本文由 发表于 2023年1月9日 00:11:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定