【发布时间】:2017-06-06 09:59:02
【问题描述】:
我想知道是否有可能使用 boost asio 库访问网络服务。
我尝试了从 boost asio 文档中获得的以下代码(在 IOS,C++11 中)。 但它抛出了以下内容。
try
{
boost::asio::io_service io_service;
std::string address = "http://www.thomas-bayer.com/axis2/services/BLZService/";
// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
tcp::resolver::query query(address,boost::asio::ip::resolver_query_base::numeric_service);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
endpoint_iterator->host_name() = "www.thomas-bayer.com/axis2/services/BLZService/";
std::cout<<"Print Query --"<<std::endl;
// 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 << "POST: HTTP/1.0\r\n";
request_stream << "Host: " << address << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
// 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/")
{
std::cout << "Invalid response\n";
return;
}
if (status_code != 200)
{
std::cout << "Response returned with status code " << status_code << "\n";
return;
}
// 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")
std::cout << header << "\n";
std::cout << "\n";
// Write whatever content we already have to output.
if (response.size() > 0)
std::cout << &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))
std::cout << &response;
if (error != boost::asio::error::eof)
throw boost::system::system_error(error);
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << "\n";
}
例外:连接:无法分配请求的地址
或者
异常:解决:找不到主机(权威)
代码有什么问题?还是我做错了?
谢谢
【问题讨论】:
-
我认为问题与访问Web服务无关,相反,我认为上面的代码无法解析主机名。也许这两个链接可以帮助你。 Boost not working over network 和这个boost asio: “host not found (authorative)”
-
解析器使用主机名而不是 URL。使用
thomas-bayer.com作为解析器。 -
tcp::resolver::query query(address,boost::asio::ip::resolver_query_base::numeric_service);解决主机未找到错误但产生无法分配请求的地址。基本上我得到上述错误之一。我将尝试使用您提供的第一个链接并回来。谢谢 -
嗨@JohnTracid 我已尝试将网址设置为如下
tcp::resolver::query query("thomas-bayer.com", boost::asio::ip::resolver_query_base::numeric_service);但我无法分配请求地址错误。谢谢
标签: c++ ios boost webservices-client