6

我想在我的 C++ 程序中重试 curl 连接 5 次。当它连续 5 次失败时,它应该停止程序的执行。但是,它在此时出现第一个错误后停止。我能够捕捉到错误,但是我不知道如何执行之前的 curl 连接。例如,使用 jQuery,我可以使用类似$.ajax(this);. 对于 C++ 中的 LibCurl,我正在寻找类似的解决方案。

我当前的 LibCurl 代码如下所示,请注意,我使用了多个 curl 连接,这些连接都有其他设置,因此我想要一个通用方法,可以用于我的LibcurlError函数中的所有 LibCurl 错误,也包括在下面。

curl = curl_easy_init();
if (curl) {
    CurlResponse = "";
    host = "http://google.com";
    LibcurlHeaders = curl_slist_append(NULL, "Expect:");
    if (ProxyAddress.length() > 0) {
        curl_easy_setopt(curl, CURLOPT_PROXY, ProxyAddress.c_str());
        }
    curl_easy_setopt(curl, CURLOPT_URL, (host).c_str());
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, LibcurlHeaders);
    res = curl_easy_perform(curl);
    curl_slist_free_all(LibcurlHeaders);
    if (res != CURLE_OK) {


        //AT THIS POINT I WOULD LIKE TO RETRY FOR 5 TIMES WHICH I WOULD LIKE TO CATCH IN MY LibcurlError FUNCTION.


        LibcurlError(curl_easy_strerror(res), host);
        }
    curl_easy_cleanup(curl);
    }
curl_global_cleanup();


void LibcurlError(string error, string host) {
    //IF FAILED FOR LESS THEN 5 TIMES IN A ROW -> RETRY CURL
    //ELSE I WOULD LIKE TO EXECUTE MY ORIGINAL CODE WHICH IS STATED BELOW 

    Message = "LibCurl Error: ";
    if (error == "Couldn't resolve host name") {
        Message.append("Couldn't connect to the server of ");
        if (host.find("google.com") != string::npos) {
            Message.append("Google");
            }
        else {
            Message.append("'" + host + "'");
            }
        }
    else {
        Message.append("'" + error + "'");
        }
    cout << Message << endl;
    system("pause");
    exit(0);
    }
4

1 回答 1

0

没有专门执行此操作的 CURL 方法,因为它可以通过重复调用来完成curl_easy_perform

以下是您如何在问题中编写代码(至少是相关部分),使用循环重复重试 CURL 请求:

#include <unistd.h>
#include <curl/curl.h>

/*
 * This is the maximum number of times CURL will run
 */
const int max_attempts = 5;

curl = curl_easy_init();
if (curl) {
    CurlResponse = "";
    host = "http://google.com";
    LibcurlHeaders = curl_slist_append(NULL, "Expect:");
    if (ProxyAddress.length() > 0) {
        curl_easy_setopt(curl, CURLOPT_PROXY, ProxyAddress.c_str());
        }
    curl_easy_setopt(curl, CURLOPT_URL, (host).c_str());
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, LibcurlHeaders);
    for (int i = 1; i <= max_attempts &&
        (res = curl_easy_perform(curl)) != CURLE_OK; i++) {
         /*
          * At this point, you would sleep
          * for some seconds between requests
          */
          const int sleep_secs = 1;
          sleep(sleep_secs);
     }
    // As others have mentioned, you should delete this line:
    //curl_slist_free_all(LibcurlHeaders);

    if (res != CURLE_OK) {
        // The max retries have all failed
        LibcurlError(curl_easy_strerror(res), host);
    }
    else {
        // The request has succeeded in the first `max_retries` attempts
        // ...
    }
    curl_easy_cleanup(curl);
}
curl_global_cleanup();
于 2022-01-27T05:50:41.843 回答