首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用cpp-httplib发出HTTPS请求

使用cpp-httplib发出HTTPS请求
EN

Stack Overflow用户
提问于 2020-08-25 18:12:54
回答 1查看 3.8K关注 0票数 0

我试图在c++中使用Cpp-httplib发送HTTPS请求,但是我收到了各种错误和警告,构建它们的示例

代码语言:javascript
复制
#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;
}

这是构建输出:

代码语言:javascript
复制
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,但我不知道如何修复它。

EN

Stack Overflow用户

回答已采纳

发布于 2020-08-27 17:57:31

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

代码语言:javascript
复制
#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;
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63584828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档