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

静态curl库编译与使用(c++)

静态curl库编译与使用

静态curl库编译与使用:mingw https://curl.se/windows/

// 测试:设置URL地址
// curl_easy_setopt(curlHandle, CURLOPT_URL, “https://ipinfo.io/json”);
// curl_easy_setopt(curlHandle, CURLOPT_SSL_VERIFYPEER, 0L);
// curl_easy_setopt(curlHandle, CURLOPT_SSL_VERIFYHOST, 0L);

#include <iostream>#include <chrono>
#include <iomanip>
#include <sstream>std::string log_time()
{time_t t = time(nullptr);char tmp[25]{};struct tm* timinfo = localtime(&t);strftime(tmp, sizeof(tmp), "%Y-%m-%dT%H:%M:%S", timinfo);return tmp;
}#include <curl/curl.h>
#pragma comment(lib, "libbrotlicommon.a")
#pragma comment(lib, "libbrotlidec.a")
#pragma comment(lib, "libcrypto.a")#pragma comment(lib, "libcurl.a")
#pragma comment(lib, "libnghttp2.a")
#pragma comment(lib, "libnghttp3.a")
#pragma comment(lib, "libngtcp2.a")
#pragma comment(lib, "libngtcp2_crypto_quictls.a")
#pragma comment(lib, "libpsl.a")
#pragma comment(lib, "libssh2.a")
#pragma comment(lib, "libssl.a")
#pragma comment(lib, "libz.a")
#pragma comment(lib, "libzstd.a")#pragma comment(lib, "libmingwex.a")
#pragma comment(lib, "libgcc.a")#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "bcrypt.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "Normaliz.lib")
#pragma comment(lib, "legacy_stdio_definitions.lib")#include <iostream>
#include <string>// 定义回调函数,用于处理收到的响应数据
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response) {size_t totalSize = size * nmemb;response->append((char*)contents, totalSize);return totalSize;
}int main() {CURL* curlHandle;CURLcode res;// 初始化Curlcurl_global_init(CURL_GLOBAL_DEFAULT);// 创建Curl会话curlHandle = curl_easy_init();if (curlHandle == nullptr) {std::cout << "Failed to create a new CURL handle." << std::endl;return -1;}try {// 设置URL地址curl_easy_setopt(curlHandle, CURLOPT_URL, "https://ipinfo.io/json");curl_easy_setopt(curlHandle, CURLOPT_SSL_VERIFYPEER, 0L);curl_easy_setopt(curlHandle, CURLOPT_SSL_VERIFYHOST, 0L);//curl_easy_setopt(curlHandle, CURLOPT_SSL_VERIFYPEER, 1L);//winssl编译时使用windows自带的根证书//curl_easy_setopt(curlHandle, CURLOPT_SSL_VERIFYHOST, 2L);//curl_easy_setopt(curlHandle, CURLOPT_MAXREDIRS, 5);//curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1);//curl_easy_setopt(curlHandle, CURLOPT_NOSIGNAL, 1L);//curl_easy_setopt(curlHandle, CURLOPT_AUTOREFERER, 1L);//curl_easy_setopt(curlHandle, CURLOPT_POST, false);//curl_easy_setopt(curlHandle, CURLOPT_FAILONERROR, 1L);//curl_easy_setopt(curlHandle, CURLOPT_CONNECTTIMEOUT, 30L);//curl_easy_setopt(curlHandle, CURLOPT_LOW_SPEED_TIME, 60L);//curl_easy_setopt(curlHandle, CURLOPT_LOW_SPEED_LIMIT, 30L);// 设置写入回调函数std::string responseData;curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, WriteCallback);curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &responseData);// 发送GET请求res = curl_easy_perform(curlHandle);if (res != CURLE_OK) {std::cerr << "Error occurred while sending request: " << curl_easy_strerror(res) << std::endl;throw - 1;}else {// 打印响应结果std::cout << "Response data:\n" << responseData << std::endl;}}catch (...) {// 错误处理std::cerr << "An error occurred during the process." << std::endl;return -1;}// 清理资源curl_easy_cleanup(curlHandle);curl_global_cleanup();return 0;
}
http://www.lryc.cn/news/301844.html

相关文章:

  • element 表单提交图片(表单上传图片)
  • Android 15 第一个开发者预览版
  • anomalib1.0学习纪实-续1:增加新算法
  • Java+Vue+MySQL,国产动漫网站全栈升级
  • 机器人常用传感器分类及一般性要求
  • C++-opencv的imread、imshow、waitkey、namedWindow
  • 开源语音识别faster-whisper部署教程
  • 使用IntelliJ IDEA配置Maven (入门)
  • 汽车金融市场研究:预计2029年将达到482亿美元
  • 关于举办第十五届蓝桥杯大赛电子赛5G全网规划与建设赛项的通知
  • Vue3快速上手(七) ref和reactive对比
  • 8、内网安全-横向移动RDPKerberos攻击SPN扫描WinRMWinRS
  • 《数据结构与算法之美》读书笔记
  • C语言—字符数组(3)
  • linux 实用技能
  • 【maya 入门笔记】基本视图和拓扑
  • IO 流分类
  • JVM的主要组成部分,以及它们的作用。JVM中的内存区域有哪些,它们各自的作用是什么?什么是Java的堆内存,它如何影响程序的性能?
  • Qt QWidget以及各种控件、布局 核心属性(适合入门使用时查询)
  • svg图片构造QGraphicsSvgItem对象耗时很长的问题解决
  • 边坡位移监测设备:守护工程安全的前沿科技
  • Qt使用单例模式读取xml文件
  • 备战蓝桥杯 Day6(学习动态规划)
  • 【uniapp】自定义步骤条样式
  • UE5 C++ UObject实例化
  • Appium环境安装与架构介绍
  • Vue+Vite项目初建(axios+Unocss+iconify)
  • ASUS华硕枪神8笔记本电脑G614JIR,G814JVR,G634JYR,G834JZR工厂模式出厂Windows11系统 带重置还原功能
  • Python入门:常用模块—xml模块
  • 蓝队应急响应工具箱v2024.1​