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

#include 
#include 
#include 

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 
#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 
#include 

// 定义回调函数,用于处理收到的响应数据
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;
}

你可能感兴趣的:(c++,curl,静态库)