2

I'm trying to use Microsoft's cpprestsdk, but I'm getting this error :

HTTP Exception :: Failed to connect to any resolved endpoint Code :: 101

Here is my code :

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>

using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams

int main(int argc, char* argv[])
{
    auto fileStream = std::make_shared<ostream>();

    // Open stream to output file.
    pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
    {
        *fileStream = outFile;

        // Create http_client to send the request.
        http_client client(U("http://www.bing.com/"));

        // Build request URI and start the request.
        uri_builder builder(U("search"));
        builder.append_query(U("q"), U("cpprestsdk github"));
        return client.request(methods::GET, builder.to_string());
    })

    // Handle response headers arriving.
    .then([=](http_response response)
    {
        printf("Received response status code:%u\n", response.status_code());

        // Write response body into the file.
        return response.body().read_to_end(fileStream->streambuf());
    })

    // Close the file stream.
    .then([=](size_t)
    {
        return fileStream->close();
    });

    // Wait for all the outstanding I/O to complete and handle any exceptions
    try
    {
        requestTask.wait();
    }
    catch (web::http::http_exception &e)
    {
        printf("HTTP Exception :: %s\nCode :: %d\n", e.what(), e.error_code());
    }
    catch (const std::exception &e)
    {
        printf("Error exception:%s\n", e.what());
    }

    return 0;
}

Source : https://github.com/Microsoft/cpprestsdk/wiki/Getting-Started-Tutorial

And I used this

g++ -std=c++11 my_file.cpp -o my_file -lboost_system -lcrypto -lssl -lcpprest
./my_file

to build my code. But I'm getting an error code of 101. What is going wrong?

Nikhil Wagh
  • 1,376
  • 1
  • 24
  • 44
  • 1
    cpprest uses boost asio, in your case, and this is `asio::error::network_unreachable 101`. Are you sure you have network access? (https://github.com/Microsoft/cpprestsdk/blob/7db35851c16ecbe79a42f4ea4647cef647b7bf2c/Release/src/http/client/http_client_asio.cpp#L901) – Mihayl Mar 26 '18 at 07:05
  • 1
    Are you using proxy to access internet? Maybe you need to configure in your `http_client` `web::http::client::http_client_config cfg; cfg.set_proxy(web::web_proxy(U("enter your proxy url here")));` and passing in the to the client constructor `http_client client(U("http://www.bing.com/"), gf);`. – Mihayl Mar 26 '18 at 07:24
  • Okay thanks. I was using a proxy. I changed my network and it is working now. – Nikhil Wagh Mar 26 '18 at 08:55

1 Answers1

3

cpprest uses boost asio [1], in your case, and this is asio::error::network_unreachable 101. Make sure you have internet access (e.g. try ping the url).

If you're using proxy to access internet you need to configure it in your http_client:

web::http::client::http_client_config cfg;
cfg.set_proxy(web::web_proxy(U("enter your proxy url here")));

// and passing in the to the client constructor
http_client client(U("http://www.bing.com/"), cfg);
Mihayl
  • 3,821
  • 2
  • 13
  • 32