从 [/ edit] code copied运行Cpp-netlib(版本0.11-rc1) [编辑(添加)] HttpClient example时没完。
我认为是因为连接处于打开状态。 1.我的假设是否正确? 2.是否必须手动关闭连接? 3.如果是,如何访问连接?
答案 0 :(得分:6)
Cpp-netlib http_client
似乎使用了asio::io_service
的keeps running。
要完成HttpClient程序,请使用asio::io_service::stop()
。
能够访问io_service
使用的http_client
:
io_service
个实例; http_client_options
将其提供给http_client
;和stop()
- 实例上调用io_service
。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。)