【问题标题】:making HTTPS request using cpp-httplib使用 cpp-httplib 发出 HTTPS 请求
【发布时间】:2020-12-14 11:57:11
【问题描述】:

我正在尝试使用 Cpp-httplib 在 c++ 中发送 HTTPS 请求,但在构建 example 时遇到了各种错误和警告:

#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 的配置无关,因为我已经创建了自我证书。我认为问题出在 cpp-httplib 上,但我不知道如何解决。

【问题讨论】:

    标签: c++ https openssl


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 2016-04-29
      • 2023-03-26
      • 2013-01-05
      相关资源
      最近更新 更多