简体   繁体   English

从客户端向服务器发送数据 response_tak = client.request(req) 如果在特定时间没有响应,我想添加超时功能

[英]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);如果响应不是来自client.request(request); or if it is taking too much time then My.exe will just wait continiously?或者如果花费太多时间,那么 My.exe 将继续等待? 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.在 cpprestsdk 库中有这个 function 但没有太多关于这个http_client_config类的utility::seconds web::http::client::http_client_config::timeout()const function。

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 .您可以通过创建 http_client_config object 并使用void web::http::client::http_client_config::set_timeout ( const T & timeout )为所有请求设置超时, 文档 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 );然后你需要将配置 class 作为第二个参数传递给构造函数,使用你提到的方法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. class pplx::task<web::http::http_response>是异步的,如果你直接调用.Get()它将被阻塞。 You should either check if the response is already there with您应该检查响应是否已经存在

bool done = resp.is_done();

or use a callback function或使用回调 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).如果 is_done() 返回 false,调用 get() 将有阻塞线程的风险,这首先违背了使用异步 API 的目的(它将阻止 GUI 刷新和服务器缩放)。 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.对于这种情况,我们需要采取不同的方法:将处理程序 function 附加到任务,任务完成后将调用该处理程序。 We do this using the then() function我们使用 then() function 来做到这一点

more information 更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM