linux下libcurl的https简单例子

1、安装openssl

复制代码
sudo apt update
sudo apt install build-essential libssl-dev libtool autoconf automake zlib1g-dev

2、编译安装libcurl

复制代码
wget https://curl.se/download/curl-7.69.1.tar.gz
./configure --with-openssl --enable-shared --enable-static --prefix=/opt/curl-7.69.1/build
make -j4 && make install

3、从远端导出证书

复制代码
openssl s_client -connect 192.168.8.1:443 </dev/null 2>/dev/null | openssl x509 > device.crt

4、CMakeLists.txt添加依赖:

复制代码
target_link_libraries(Test libcurl.a crypto ssl)

5、实现HTTPS GET方法

复制代码
#pragma once
#include "Config.h"
#include <string>
#include <curl/curl.h>
#include <functional>

namespace uniseas {
namespace tv {

class HttpUtils
{
public:
	using Handler = std::function<void(const std::string&)>;
    HttpUtils(const Config& conf, Handler&& handler);
    ~HttpUtils();
	
	void DoHttpGet(const std::string& url);

    static size_t WriteResponse(void* buffer, size_t size, size_t nmemb, void* lpVoid);

private:
    const Config& m_conf;
	std::string m_baseUrl;
	Handler m_handler;
};

}
}

#include <fstream>
#include <iostream>
#include <thread>
#include "HttpUtils.h"

namespace uniseas {
namespace tv {

HttpUtils::HttpUtils(const Config& conf, Handler&& handler)
    : m_conf(conf)
	, m_handler(std::move(handler))
{
    m_baseUrl = "https://";
    m_baseUrl += m_conf.get_http_conn().ipAddress;
    m_baseUrl += ":";
    m_baseUrl += std::to_string(m_conf.get_http_conn().port);
    m_baseUrl += "/";

	std::cout << "m_baseUrl:" << m_baseUrl << std::endl;
}


HttpUtils::~HttpUtils()
{
}

// 下载openssl:sudo apt install build-essential libssl-dev libtool autoconf automake zlib1g-dev
// 导出证书: openssl s_client -connect 192.168.8.1:443 </dev/null 2>/dev/null | openssl x509 > device.crt
void HttpUtils::DoHttpGet(const std::string& url)
{
	CURL *pCurlHandle = curl_easy_init();
    curl_easy_setopt(pCurlHandle, CURLOPT_URL, url.c_str());
    curl_easy_setopt(pCurlHandle, CURLOPT_WRITEFUNCTION, WriteResponse);
	std::string responseHead;
    curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &responseHead);

	curl_easy_setopt(pCurlHandle, CURLOPT_CAINFO, "./device.crt");
	curl_easy_setopt(pCurlHandle, CURLOPT_SSL_VERIFYPEER, 1L);
	curl_easy_setopt(pCurlHandle, CURLOPT_SSL_VERIFYHOST, 2L);

	// 调试日志打印
	// curl_easy_setopt(pCurlHandle, CURLOPT_VERBOSE, 1L);

    curl_easy_setopt(pCurlHandle, CURLOPT_TIMEOUT, 10L);

    CURLcode res = curl_easy_perform(pCurlHandle);
    if (res != CURLE_OK) {
        std::cerr << "Request failed: " << curl_easy_strerror(res) << std::endl;
    } else {
        long httpCode = 0;
        curl_easy_getinfo(pCurlHandle, CURLINFO_RESPONSE_CODE, &httpCode);
        std::cout << "HTTP Status: " << httpCode << " Response:" << responseHead << std::endl;
    }

	curl_easy_cleanup(pCurlHandle);
}

size_t HttpUtils::WriteResponse(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
    std::string* str = static_cast<std::string *>(lpVoid);
    if (NULL == str || NULL == buffer)
    {
        return -1;
    }

    char* pData = static_cast<char *>(buffer);
    str->append(pData, size * nmemb);

    return nmemb;
}

}
}
相关推荐
ApacheSeaTunnel26 分钟前
一套替代四套!同程旅行用 Apache SeaTunnel 搭建多模态统一数据通道
大数据·数据库·ai·开源·数据集成·seatunnel·技术分享·数据同步
段一凡-华北理工大学29 分钟前
向量数据库实战:选型、调优与落地~系列文章02:Embedding 嵌入模型选型指南:OpenAI、BGE、Jina、Cohere 横评
大数据·数据库·人工智能·embedding·高炉炼铁·jina·工业智能体
️学习的小王32 分钟前
MySQL 数据库基础操作全解:从建库到增删改查
数据库·mysql·oracle
Spider Cat 蜘蛛猫44 分钟前
OIDC 联邦 SSO(复旦 OP / 清华 RP)完整全链路(纯标准OIDC、无User Storage SPI、含用户浏览器操作)
数据库·iam
执行x1 小时前
wsl2安装+桥接模式+固定IP
linux·windows·ubuntu
oradh1 小时前
Oracle 11g单库升级至19c单库(dbua图形方式)
数据库·oracle·11g升级19c·dbua升级方式
王九思1 小时前
对称加密与非对称加密详解
java·网络·数据库
周杰伦的稻香1 小时前
(1071, ‘Specified key was too long; max key length is 767 bytes‘)
数据库·mysql
Leo.yuan2 小时前
数据目录和数据字典有什么区别?一文讲清
大数据·数据库·人工智能
全堆鸿蒙2 小时前
鸿蒙应用开发实战【37】— 数据统计:countByStatus 聚合查询
数据库·华为·harmonyos·鸿蒙系统