1

我正在尝试在 C++ 中设置一个简单的 HTTPS 客户端,以针对Reqres API 测试一些功能。

我使用cpp-netlib作为主要的 C++ 网络库。根据文档中提供的示例,我编写了一些代码来执行一些 GET 查询:

#ifndef BOOST_NETWORK_ENABLE_HTTPS
#    define BOOST_NETWORK_ENABLE_HTTPS
#endif // !BOOST_NETWORK_ENABLE_HTTPS

#include <boost/network/protocol/http/client.hpp>
#include <iostream>

using namespace boost::network::http;

int main() {
    client::options options;
    options.follow_redirects(true)
        .cache_resolved(true)
        .timeout(10);
    client client_(options);

    client::request request_("https://reqres.in/api/users");
    client::response response_ = client_.get(request_);
    std::cout << body(response_) << std::endl;
    return 0;
}

当我运行代码时,我得到了这个异常:

libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::system::system_error>: sslv3 alert handshake failure

我认为问题与 OpenSSL 配置有关。我试图通过生成 CSR 和 KEY 来生成要在握手中使用的证书:

openssl req -new -newkey rsa:4096 -nodes -keyout mohabouje.key -out mohabouje.csr

然后是 PEM:

openssl x509 -req -sha256 -days 365 -in mohabouje.csr -signkey mohabouje.key -out mohabouje.pem

我已经修改了代码,以在客户端选项中使用这些文件:

client::options options;
options.follow_redirects(true)
    .cache_resolved(true)
    .openssl_certificate_file("/opt/ssl/mohabouje.csr")
    .openssl_private_key_file("/opt/ssl/mohabouje.key")
    .timeout(10);

但是现在,我得到了这个例外:

libc++abi.dylib: terminating with uncaught exception of type boost::wrapexcept<boost::system::system_error>: use_certificate_file: no start line

另一种方法,我试过这个:

client::options options;
options.follow_redirects(true)
   .cache_resolved(true)
   .openssl_certificate_file("/opt/ssl/mohabouje.pem")
   .openssl_private_key_file("/opt/ssl/mohabouje.key")
   .timeout(10);

但是,我得到了同样的例外:

libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::system::system_error>: sslv3 alert handshake failure

我究竟做错了什么?任何人都可以提供一个针对这个公共 API 的简单查询的工作示例吗?

4

0 回答 0