当前位置: 首页 > news >正文

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;
}}
}
http://www.dtcms.com/a/614270.html

相关文章:

  • 网站建立的意义建立网站可以赚钱吗?
  • 全连接层详解:从原理到应用的全面解析
  • Docker-Dockerfile 完全指南:编写最佳实践的镜像
  • 百度智能云建站广州地址设计网站
  • macOS系统中使用clang/clang++编译Skia源码的方法
  • 建设银行的网站怎么打开桂林建网站哪家好
  • 在线教育网站策划方案苏州做网站最好公司有哪些
  • 网站制作需要网站制作wordpress内容付费模板
  • OpenCV(二十六):高斯滤波
  • LeetCode Hot100 接雨水
  • C#22、什么是IEnumerable
  • 网站建立数据库手机ftp传网站文件在哪里
  • Flume Kafka源与汇的topic覆盖问题解决
  • 基于卷积神经网络的手写数字识别
  • 旅游网站建设ppt模板下载宁国新站seo
  • 助贷获客系统哈尔滨网站推广优化公司
  • 前端国际化解决方案,i18n库推荐
  • Intellij idea 注释模版
  • C语言指针的详细讲解应用(江科大)
  • 哪方面的网站小程序推广方案
  • 张家口全景网站建设百度新闻官网首页
  • 轻量不卡顿!7-Zip 清爽压缩软件:高压缩比
  • 易语言开发编译器 | 高效简洁的开发工具,让编程更轻松
  • Android中的后台任务最佳实践
  • 自适应企业网站模板重庆网站建站一站式服务
  • 关于C语言的电子书,有需要的关注联系我
  • MinGW-w64 工具链(GCC 编译器) 的不同构建版本的区别
  • 【C++】 set/multiset底层原理与逻辑详解
  • 易语言exe反编译器:深度解析与使用指南
  • 19.优先级队列容器priority_queue