2

I wonder whether there is a possibility to access a web service using boost asio library.

I have tried following code (in IOS, C++11) which I got from boost asio documentation. But it throws the following.

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";
}

Exception: connect: Can't assign requested address

Or

Exception: resolve: Host not found (authoritative)

What is wrong with the code? Or I am doing completely wrong?

Thanks

Kid
  • 169
  • 1
  • 19
  • I think the problem is not related to access a web service, instead, I think the code above cannot resolve the hostname. Maybe this two links can help you. [Boost not working over network](https://stackoverflow.com/questions/26951924/boost-not-working-over-network) and this [boost asio: “host not found (authorative)”](https://stackoverflow.com/questions/12542460/boost-asio-host-not-found-authorative) – JTejedor Jun 06 '17 at 10:17
  • Resolver works with host name not URL. Use `thomas-bayer.com` for resolver. – John Tracid Jun 06 '17 at 10:26
  • `tcp::resolver::query query(address,boost::asio::ip::resolver_query_base::numeric_service);` solve host not found error but produce cant assign requested address. basically I get either one of the above mentioned error. I will try with the first link u have given and come back. Thanks – Kid Jun 06 '17 at 10:27
  • Hi @JohnTracid I have tried setting the url as follows `tcp::resolver::query query("thomas-bayer.com", boost::asio::ip::resolver_query_base::numeric_service);` but i got cant assign requestes address error. Thanks – Kid Jun 06 '17 at 11:45

1 Answers1

4

The name resolution fails, because you are confusing what is a request, a URL, a protocol, hostname and an IP address.

Do the requestion on a FQDN. You need to supply a service unless you know it. The service in this case follows from the the protocol¹, `http:// is usually served on port 80:

std::string const address = "www.thomas-bayer.com";
tcp::resolver::query query(address, "80", boost::asio::ip::resolver_query_base::numeric_service);

Note that on most systems you can equivalently use:

std::string const address = "www.thomas-bayer.com";
tcp::resolver::query query(address, "http");

See where the http:// and www.thomas-bayer.com parts went?

Now /axis2/services/BLZService/ is the query path, as you would write in a GET request:

request_stream << "GET /axis2/services/BLZService/ HTTP/1.1\r\n";

Notes:

  1. POST is not a header (so no colon!) it's a HTTP "verb" (GET, POST, DELETE, PUT...)
  2. The "Host" header is a host name:

     request_stream << "Host: " << address << "\r\n";
    

    was correct iff address was really the logical name for the host

  3. setting the hostname like this:

     endpoint_iterator->host_name() = "www.thomas-bayer.com/axis2/services/BLZService/";
    

    is something I've never seen before and I'm not sure what it should achieve. Perhaps it's just wrong?

¹ by convention, it could be other

Fixes

#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(); }
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Hi Actually I tried to POST my request to the web service, but why did you change to GET instead? – Kid Jun 07 '17 at 10:05
  • @Kid why not? I just forgot to change it back. I tend to _TEST_ my code before posting an answer, and I'm not going to send POST requests to a server I don't know anything about. – sehe Jun 07 '17 at 10:06
  • Oh ok. It return for the method ` GET -- Response returned with status code 500` And for `POST -- Exception: read_until: End of file`. But I yet to add the actual xml request. Probably this causes those error. I am currently working on it. Thanks – Kid Jun 07 '17 at 10:26