3

我正在考虑将 cpp netlib 用于新项目。所有示例都显示以阻塞方式读取响应正文:

client::response response_ = client_.get(request_);
std::string body_ = body(response_);

如果我使用 async 标签构造我的客户端对象:

basic_client<http_async_8bit_udp_resolve_tags, 1, 1> client_();

那有什么影响?

是否可以将body包装器的结果作为boost::shared_future<std::string>?

我只需要将阻塞调用包装在它自己的线程中吗?

4

1 回答 1

1

查看当前http客户端文档:http ://cpp-netlib.org/0.12.0/reference/http_client.html

  1. http 客户端现在默认总是异步的
  2. 您可以选择在getorpost调用中提供回调函数或对象:

    struct body_handler {
    
        explicit body_handler(std::string & body)
        : body(body) {}
    
        BOOST_NETWORK_HTTP_BODY_CALLBACK(operator(), range, error) {
            // in here, range is the Boost.Range iterator_range, and error is
            // the Boost.System error code.
            if (!error)
                body.append(boost::begin(range), boost::end(range));
        }
    
        std::string & body;
    };
    
    // somewhere else
    std::string some_string;
    response_ = client_.get(request("http://cpp-netlib.github.com/"),
                        body_handler(some_string));
    
  3. client::response对象已经封装了对象future

响应对象封装了一旦值可用就会被填充的期货。

于 2017-12-05T03:25:38.963 回答