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

}
}
相关推荐
神梦流几秒前
GE 引擎的内存优化终局:静态生命周期分析指导下的内存分配与复用策略
linux·运维·服务器
凡人叶枫3 分钟前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
啦啦啦_99996 分钟前
Redis-5-doFormatAsync()方法
数据库·redis·c#
wdfk_prog8 分钟前
[Linux]学习笔记系列 -- [drivers][input]serio
linux·笔记·学习
生产队队长14 分钟前
Redis:Windows环境安装Redis,并将 Redis 进程注册为服务
数据库·redis·缓存
老邓计算机毕设15 分钟前
SSM找学互助系统52568(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架·javaweb 毕业设计
痴儿哈哈18 分钟前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python
xuhe225 分钟前
[全流程详细教程]Docker部署ClawBot, 使用GLM4.7, 接入TG Bot实现私人助理. 解决Docker Openclaw Permission Denied问题
linux·docker·ai·github·tldr
Lsir10110_41 分钟前
【Linux】进程信号(下半)
linux·运维·服务器
Σίσυφος19001 小时前
PCL法向量估计 之 方向约束法向量(Orientation Guided Normal)
数据库