Libcurl发布请求在Windows 7上不起作用

vuktfyat  于 2023-02-10  发布在  Windows
关注(0)|答案(2)|浏览(225)

我正在尝试向https REST API发出简单的POST请求,并在Windows 7操作系统上接收响应。防火墙已停用,证书位于.exe目录中。
下面是我正在使用的代码:

std::string postRequest(std::string json, std::string endpoint,std::string code,std::string patchOrPOST,std::string source)
{
    CURL* curl;
    CURLcode res;
    std::string response;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    std::string authorization = "Authorization: Basic " + code, portApiHttp = "Referer: " + portAPI + "/index.html", origin = "Origin: " + portAPI;//X-Requested-With: XMLHttpRequest
    struct curl_slist* headers = NULL;
    if (curl) {
        headers = curl_slist_append(headers, "Connection: keep-alive");
        headers = curl_slist_append(headers, "Content-Length: "+(int)strlen(json.c_str()));
        headers = curl_slist_append(headers, authorization.c_str());
        headers = curl_slist_append(headers, "Accept: application/json");
        headers = curl_slist_append(headers, "Content-Type: application/json");
        if (source.size() != 0)
        {
            headers = curl_slist_append(headers, source.c_str());
        }
        headers = curl_slist_append(headers, R"(sec-ch-ua: "Chromium";v="91")");
        headers = curl_slist_append(headers, "sec-ch-ua-mobile: ?0");
        headers = curl_slist_append(headers, "X - Requested - With: XMLHttpRequest"); 
        headers = curl_slist_append(headers, origin.c_str());
        headers = curl_slist_append(headers, "Sec-Fetch-Site: same-origin");
        headers = curl_slist_append(headers, "Sec-Fetch-Mode: cors");
        headers = curl_slist_append(headers, "Sec-Fetch-Dest: empty");
        headers = curl_slist_append(headers, portApiHttp.c_str());
        headers = curl_slist_append(headers, "Accept-Encoding: gzip, deflate, br");
        headers = curl_slist_append(headers, "Accept-Language: en-US,en;q=0.9");
        curl_easy_setopt(curl, CURLOPT_URL,endpoint.c_str());
        curl_easy_setopt(curl, CURLOPT_CAINFO, "ca-bundle.crt");
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
        if (patchOrPOST == "PATCH")
        {
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
        }
        else if (patchOrPOST == "PUT")
        {
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
        }
        else if (patchOrPOST == "GET")
        {
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
        }
        else if(patchOrPOST=="DELETE")
        {
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
        }
        //curl_easy_setopt(curl, CURLOPT_PROXY, "127.0.0.1:8888");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5L);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 50L);
        curl_easy_setopt(curl, CURLOPT_HEADER, 1);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
        if (patchOrPOST != "GET")
        {
            curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, json.size());
            curl_easy_setopt(curl, CURLOPT_POST, 1);
        }
        res= curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        if (res!= CURLE_OK) {
            curl_easy_cleanup(curl);
            return "ERROR";
        }
        res= curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
        if (res!= CURLE_OK) {
            curl_easy_cleanup(curl);
            return "ERROR";
        }
        res = curl_easy_perform(curl);
        long http_code;
        /* Check for errors */
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));
        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    return response;
}

为什么此代码在Windows 10上可以工作,但在Windows 7上存在SSL问题?

wnavrhmk

wnavrhmk1#

为什么此代码在Windows 10上可以工作,但在Windows 7上存在SSL问题?
很可能是服务器请求的加密套件比Windows 7所能提供的更强大。

3vpjnl9f

3vpjnl9f2#

我在使用Windows 7和libcurl时遇到了类似的问题。我已经找到了解决方案。我在使用schannel时使用了libcurl(使用vcpkg时默认为Windows)。当我用openssl构建libcurl时,问题得到了解决。
如果您使用的是vcpkg,则可以使用以下命令将libcurlopenssl一起安装:vckpg.exe install curl[openssl]
你也可以从curl站点下载一个预编译的二进制文件。我从这个默认的curl windows构建中得到了解决这个问题的提示:
https://github.com/curl/curl-for-win

相关问题