C libcurl错误重试

C++ LibCurl retry on error

本文关键字:重试 错误 libcurl      更新时间:2023-10-16

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

我当前的libcurl代码如下所示,请注意,我使用的多个卷曲连接都具有其他设置,因此我想要一种通用方法,我可以在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);
    }

没有专门这样做的卷曲方法,因为可以通过重复调用curl_easy_perform来完成。

这是您如何使用循环在问题(相关部分(中编写代码来反复重试卷曲请求的方式:

#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();