CURL请求失败重试方法

CURL Request Failure retry method

本文关键字:方法 重试 失败 请求 CURL      更新时间:2023-10-16

我使用CURL库进行POST、GET、下载和上传数据。出于某种原因,如果请求失败,我们计划再次重试该请求。我们计划重试5次,即使失败了,我们也会停止并向用户显示失败消息。为此,我们以10秒的延迟在循环中运行。

我的问题。

1) 是我的做法是正确的。

2) 什么是最佳实践。

更新:

int _tmain(int argc, _TCHAR* argv[])
{
    CURL *curl;
    CURLcode res;
    int nRetryCount = 0;
    do 
    {
        curl_global_init(CURL_GLOBAL_DEFAULT); 
        curl = curl_easy_init();
        if(curl) 
        {
            curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/");
            res = curl_easy_perform(curl);
            curl_easy_cleanup(curl);
            if(CURLE_OK == res) 
            {
                break;
            }
            nRetryCount++;
            if (nRetryCount < 5)
            {
                //wait for 10 sec.
                Sleep(10000);
            }       
        }
    } while (nRetryCount < 5);

    curl_global_cleanup();
    return 0;
}

您最好检查http状态代码。

curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &http_status);
if (http_status == ...) {
    break;
}