静态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;

    // 初始化Curl
    curl_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;
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值