C++ 如何使用Boost::Asio访问web服务?

C++ 如何使用Boost::Asio访问web服务?,c++,ios,boost,webservices-client,C++,Ios,Boost,Webservices Client,我想知道是否有可能使用boost asio库访问web服务 我尝试了以下代码(在IOS中,C++11),这些代码是我从boost asio文档中获得的。 但它抛出了以下内容 try { boost::asio::io_service io_service; std::string address = "http://www.thomas-bayer.com/axis2/services/BLZService/"; // Get a list of endpoints c

我想知道是否有可能使用boost asio库访问web服务

我尝试了以下代码(在IOS中,C++11),这些代码是我从boost asio文档中获得的。 但它抛出了以下内容

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";
}
试试看
{
boost::asio::io_服务io_服务;
std::字符串地址=”http://www.thomas-bayer.com/axis2/services/BLZService/";
//获取与服务器名称对应的端点列表。
tcp::解析器解析器(io_服务);
tcp::resolver::query query(地址,boost::asio::ip::resolver\u query\u base::numeric\u服务);
tcp::resolver::iterator endpoint_iterator=resolver.resolve(查询);
endpoint_iterator->host_name()=“www.thomas-bayer.com/axis2/services/BLZService/”;

名称解析失败,因为您混淆了什么是请求、URL、协议、主机名和IP地址

在上执行请求。除非您知道,否则您需要提供一个服务。在本例中,服务遵循协议\,`http://通常在端口80上提供:

std::string const address = "www.thomas-bayer.com";
tcp::resolver::query query(address, "80", boost::asio::ip::resolver_query_base::numeric_service);
请注意,在大多数系统上,您可以等效地使用:

std::string const address = "www.thomas-bayer.com";
tcp::resolver::query query(address, "http");
查看
http://
www.thomas-bayer.com
零件的去向

现在,
/axis2/services/BLZService/
是查询路径,正如您在GET请求中所写:

request_stream << "GET /axis2/services/BLZService/ HTTP/1.1\r\n";
这是我以前从未见过的东西,我不确定它应该实现什么。也许它只是错了

按惯例,它可以是其他的

修复
#包括
#包括
使用boost::asio::ip::tcp;
void test()尝试{
boost::asio::io_服务io_服务;
std::string const address=“www.thomas-bayer.com”;
//获取与服务器名称对应的端点列表。
tcp::解析器解析器(io_服务);
tcp::resolver::query query(地址,“80”,boost::asio::ip::resolver\u query\u base::numeric\u服务);
tcp::resolver::iterator endpoint_iterator=resolver.resolve(查询);

std::难道我认为问题与访问web服务无关,相反,我认为上面的代码无法解析主机名。也许这两个链接可以帮助您。并且此解析程序使用的是主机名而不是URL。使用
thomas bayer.com
作为解析程序。
tcp::Resolver::query query(地址,boost::asio::ip::解析器\查询\基::数字\服务)
solve host not found error,但product无法分配请求的地址。基本上,我得到了上述任一错误。我将尝试使用您提供的第一个链接并返回。ThanksHi@JohnTracid我尝试按如下方式设置url
tcp::resolver::query query(“thomas bayer.com”,boost::asio::ip::解析器\查询\基础::数字\服务)
但我收到了无法分配请求地址的错误。谢谢。实际上我试图将我的请求发布到web服务,但为什么您改为获取?@Kid为什么不?我只是忘记了将其更改回去。我倾向于在发布答案之前测试我的代码,并且我不会将POST请求发送到我一无所知的服务器。哦,好的。它会重新发送方法“GET--Response returned,状态代码为500”的urn和
POST--Exception:read_直到:文件结尾
。但是我还没有添加实际的xml请求。可能这会导致这些错误。我正在处理它。谢谢
 request_stream << "Host: " << address << "\r\n";
 endpoint_iterator->host_name() = "www.thomas-bayer.com/axis2/services/BLZService/";
#include <boost/asio.hpp>
#include <iostream>
using boost::asio::ip::tcp;

void test() try {
    boost::asio::io_service io_service;
    std::string const address = "www.thomas-bayer.com";

    // Get a list of endpoints corresponding to the server name.
    tcp::resolver resolver(io_service);
    tcp::resolver::query query(address, "80", boost::asio::ip::resolver_query_base::numeric_service);

    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

    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 << "GET /axis2/services/BLZService 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, status_message;
    unsigned int status_code;
    std::getline(response_stream >> http_version >> status_code, 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";
}

int main() { test(); }