如何修复 CPPrestsdk 中的"error in ssl handshake"?



我正在使用带有https url的cpprestsdk"Casablanca"主分支,它可以在Windows和osx上运行,但是当我在Linux上运行它时,我收到了"错误是ssl握手"

C++ exception with description "Error in SSL handshake" thrown in the test body.

我尝试使用火狐打开此网址,它起作用了。

当我将其与HTTP URL一起使用时,它工作正常我检查了我在一个名为"http_client_asio.cpp"的文件中找到此消息的代码

void write_request()
    {
        // Only perform handshake if a TLS connection and not being reused.
        if (m_connection->is_ssl() && !m_connection->is_reused())
        {
            const auto weakCtx = std::weak_ptr<asio_context>(shared_from_this());
            m_connection->async_handshake(boost::asio::ssl::stream_base::client,
                                          m_http_client->client_config(),
                                          m_http_client->base_uri().host(),
                                          boost::bind(&asio_context::handle_handshake, shared_from_this(), boost::asio::placeholders::error),
                                          // Use a weak_ptr since the verify_callback is stored until the connection is destroyed.
                                          // This avoids creating a circular reference since we pool connection objects.
                                          [weakCtx](bool preverified, boost::asio::ssl::verify_context &verify_context)
                                          {
                                              auto this_request = weakCtx.lock();
                                              if(this_request)
                                              {
                                                  return this_request->handle_cert_verification(preverified, verify_context);
                                              }
                                              return false;
                                          });
        }
        else
        {
            m_connection->async_write(m_body_buf, boost::bind(&asio_context::handle_write_headers, shared_from_this(), boost::asio::placeholders::error));
        }
    }
    void handle_handshake(const boost::system::error_code& ec)
    {
        if (!ec)
        {
            m_connection->async_write(m_body_buf, boost::bind(&asio_context::handle_write_headers, shared_from_this(), boost::asio::placeholders::error));
        }
        else
        {
            report_error("Error in SSL handshake", ec, httpclient_errorcode_context::handshake);
        }
    }

在客户端,我像这样渴望HTTP客户端

http_client client(U("https://www.bing.com/"));

如何修复此错误?

我在尝试连接到https URL时遇到了同样的问题。

首先,如果您不需要连接到https,则可以将网址替换为http

另一种解决方案是将http_client_config凭据验证设置为 false。像这样:

    http_client_config config;
    config.set_validate_certificates(false);

但是,如果您需要建立包含安全性的 https 连接,则必须包含本地证书文件(通常是 CRT 或 DEM 文件)并将其设置为http_client配置中的回调:

    http_client_config config;
    config.set_ssl_context_callback([]((boost::asio::ssl::context &ctx) {
        ctx.load_verify_file("PATH_TO_CERTIFICATE_FILE");
    });

您可以在此处查看有关这些功能的更多信息:

https://microsoft.github.io/cpprestsdk/classweb_1_1http_1_1client_1_1http__client__config.html

相关内容

最新更新