• 静态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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
  • 相关阅读:
    代码随想录刷题|买卖股票问题的总结
    【Python3】【力扣题】169. 多数元素
    Leecode刷题 Day7----------哈希表
    51单片机入门——I2C总线与EEPROM
    华为OD机试 - 5键键盘的输出(Java 2023 B卷 100分)
    打包时,模块的大小写很重要
    API接口接入1688电商数据平台获取商品详情数据示例
    外包干了3个多月,技术退步明显。。。。
    AES 简介 以及 C# 和 js 实现【加密知多少系列】
    【Python爬虫项目实战四】Chatgpt国内接口分享第一期
  • 原文地址:https://blog.csdn.net/qiufeng_xinqing/article/details/136157671