c++ - HTTP 异常::无法连接到任何解析的端点 - cpprestsdk

标签 c++ cpprest-sdk

我正在尝试使用 Microsoft's cpprestsdk ,但我收到此错误:

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

这是我的代码:

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

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

我用了这个

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

构建我的代码。 但我收到错误代码 101。出了什么问题?

最佳答案

cpprest 使用 boost asio [ 1 ],在您的情况下,这是 asio::error::network_unreachable 101。确保您可以访问互联网(例如尝试 ping 该网址)。

如果您使用代理访问互联网,则需要在 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);

关于c++ - HTTP 异常::无法连接到任何解析的端点 - cpprestsdk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49485466/

相关文章:

c++ - CLion 上的 CPPREST SDK 在 Mac 上使用 CMake

c++ - 如何从cpprestsdk解析json数据

c++ - 如何将流读入 Concurrency::streams::streambuf<uint8_t> 缓冲区;在 C++ 中

python - 如何使用 Cython 公开将 C++ 对象返回给 Python 的函数?

c++ - c++数组也可以泄漏内存吗?

c++ - 尝试动态增加数组大小

c++ - 卡萨布兰卡 test_runner 因 std::bad_alloc 而失败

c++ - 使用 OpenCV 将图像从 YV12 转换为 NV12

c++ - OpenCV C++ 到 C 的转换

c++ - 如何从字符串创建 web::uri 并将其放入 client.connect() 中?