使用 cpp-httplib 发出 HTTPS 请求

making HTTPS request using cpp-httplib

我正在尝试使用 Cpp-httplib but I'm getting various errors and warnings building their example:

在 C++ 中发送 HTTPS 请求
#include "stdafx.h"
#include <httplib.h>
#include <Windows.h>
#include <iostream>

#define CA_CERT_FILE "./ca-bundle.crt"
#define CPPHTTPLIB_OPENSSL_SUPPORT 1
using namespace std;


int main()
    {

    #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
        httplib::SSLClient cli("localhost", 8000);
        //httplib::SSLClient cli("google.com");
        // httplib::SSLClient cli("www.youtube.com");
        cli.set_ca_cert_path(CA_CERT_FILE);
        cli.enable_server_certificate_verification(true);
    #else
        httplib::Client cli("localhost", 8000);
    #endif
        char* x = { "hello world" };
        httplib::Params params{
            { "key", x }
        };

        auto res = cli.Post("/postReq/", params);
        if (res) {
            cout << res->status << endl;
            cout << res->get_header_value("Content-Type") << endl;
            cout << res->body << endl;
        }
        else {
    #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
            auto result = cli.get_openssl_verify_result();
            if (result) {
                cout << "error";
                cout << "verify error: " << X509_verify_cert_error_string(result)    << endl;
            }
    #endif
        }
    system("pause");
    return 0;
}

这是构建输出:

Call to 'std::basic_string::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
error C2039: 'SSLClient': is not a member of 'httplib'
error C2065: 'SSLClient': undeclared identifier
error C3861: 'X509_verify_cert_error_string': identifier not found

我是这个问题的新手,所以我将提供一些其他可能有帮助的信息:
我已经安装了 OpenSSL(x64 1.1.1g 版本)。
我在我的项目中链接了 libssl.lib 和 libcrypto.lib。 我不认为这与 OpenSSL 的配置有关,因为我已经创建了一个 self-certificate.I 认为问题出在 cpp-httplib 但我不知道如何解决它。

试试这个。定义必须在包含库之前。

#include "stdafx.h"
>>> #define CPPHTTPLIB_OPENSSL_SUPPORT
#include <httplib.h>
#include <Windows.h>
#include <iostream>

#define CA_CERT_FILE "./ca-bundle.crt"
using namespace std;


int main()
{

    #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
        httplib::SSLClient cli("localhost", 8000);
        //httplib::SSLClient cli("google.com");
        // httplib::SSLClient cli("www.youtube.com");
        cli.set_ca_cert_path(CA_CERT_FILE);
        cli.enable_server_certificate_verification(true);
    #else
        httplib::Client cli("localhost", 8000);
    #endif
        char* x = { "hello world" };
        httplib::Params params{
            { "key", x }
        };

        auto res = cli.Post("/postReq/", params);
        if (res) {
            cout << res->status << endl;
            cout << res->get_header_value("Content-Type") << endl;
            cout << res->body << endl;
        }
        else {
    #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
            auto result = cli.get_openssl_verify_result();
            if (result) {
                cout << "error";
                cout << "verify error: " << X509_verify_cert_error_string(result)    << endl;
            }
    #endif
        }
    system("pause");
    return 0;
}