与cpp-netlib的http连接有多近?

时间:2013-12-11 15:24:49

标签: c++ cpp-netlib

[/ edit] code copied运行Cpp-netlib(版本0.11-rc1) [编辑(添加)] HttpClient example时没完。

我认为是因为连接处于打开状态。 1.我的假设是否正确? 2.是否必须手动关闭连接? 3.如果是,如何访问连接?

1 个答案:

答案 0 :(得分:6)

Cpp-netlib http_client似乎使用了asio::io_servicekeeps running

要完成HttpClient程序,请使用asio::io_service::stop()

能够访问io_service使用的http_client

  1. 创建io_service个实例;
  2. 通过http_client_options将其提供给http_client;和
  3. 当程序需要完成时,请在stop() - 实例上调用io_service
  4. cppnetlib示例客户端变为:

    #include <boost/network/protocol/http/client.hpp>
    #include <boost/asio/io_service.hpp>
    #include <boost/shared_ptr.hpp>
    
    int main(int argc, char*[] argv)
    {
        using namespace boost::network;
        using namespace boost::network::http;
        using namespace boost::asio;     // LINE ADDED
    
        client::request request_("http://127.0.0.1:8000/");
        request_ << header("Connection", "close");
    
        // ADDED / MODIFIED
        boost::shared_ptr<io_service> io_service_ = boost::make_shared<io_service>();
        client client_(client::options()
                               .io_service(io_service_));
        // END ADDED
    
        client::response response_ = client_.get(request_);
        std::string body_ = body(response_);
    
        io_service_->stop();             // LINE ADDED
    }
    

    (有关完整示例,请参阅https://github.com/kaspervandenberg/https-tryout/blob/e8a918c5aa8efaaff3a37ac339bf68d132c6d2d6/httpClient.cxx。)