我想使用boost :: asio http客户端示例。
我从代码中删除了所有std :: cout,并且仍然打印到控制台。
如何禁用它们?
这是输出:
About to connect() to 172.16.7.201 port 5000 (#0)
* Trying 172.16.7.201... * connected
* Connected to 172.16.7.201 (172.16.7.201) port 5000 (#0)
> GET /v2.0/ HTTP/1.1
Host: 172.16.7.201:5000
Accept: */*
< HTTP/1.1 200 OK
< Date: Tue, 11 Aug 2015 07:48:09 GMT
...
这是代码,与没有std :: cout
的boost站点的代码完全相同Request(std::stringstream &pi_RequestStream , CrString pi_Host,CrString pi_port,std::stringstream & po_ssOutput)
{
try
{
boost::asio::io_service io_service;
// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
tcp::resolver::query query(pi_Host, pi_port);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
// Try each endpoint until we successfully establish a connection.
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
// Form the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << pi_RequestStream.str();
// Send the request.
boost::asio::write(socket, request);
// Read the response status line. The response streambuf will automatically
// grow to accommodate the entire line. The growth may be limited by passing
// a maximum size to the streambuf constructor.
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");
// Check that response is OK.
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
return E_BADRESULT;
}
if (status_code != 200)
{
return E_FAIL;
}
// Read the response headers, which are terminated by a blank line.
boost::asio::read_until(socket, response, "\r\n\r\n");
// Process the response headers.
std::string header;
while (std::getline(response_stream, header) && header != "\r");
// Write whatever content we already have to output.
if (response.size() > 0)
po_ssOutput<<&response;
// Read until EOF, writing data to output as we go.
boost::system::error_code error;
while (boost::asio::read(socket, response,
boost::asio::transfer_at_least(1), error))
po_ssOutput<<&response;
if (error != boost::asio::error::eof)
throw boost::system::system_error(error);
}
catch (std::exception& e)
{
//std::cout << "Exception: " << e.what() << "\n";
return E_FAIL;
}
return S_OK;
}