在libcurl中恢复损坏的https下载

时间:2018-04-19 10:28:17

标签: c++ curl libcurl

我有一个二进制文件要下载并在linux终端中使用curl,以下命令支持在破坏的请求上下载恢复。

curl -C - -o sample1.bin https://speed.hetzner.de/100MB.bin

以上内容将恢复已取消的下载。

当我在我的cpp程序中使用libcurl做同样的事情时,是否有任何api可以用来在HTTPS破坏的请求上实现上述结果。

谢谢你的帮助。

注意:CURL_RESUME_FROM不支持HTTPS。

2 个答案:

答案 0 :(得分:0)

我认为您可以自己实施重试系统,例如:

CURL *curl;
curl = curl_easy_init();
//Set curl options as needed with curl_easy_setopt()
char* url;
int tries = 0;
bool done = false;
while (tries != 3 && !done) {
    res = curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
    curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed);
    curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);

    if (res != CURLE_OK || http_code != 200)
        tries++;
    else
        done = true;
}
// Check if any error has occurred
if (res != CURLE_OK || http_code != 200) {
    // Could not perform request "
    if (tries == 3) {
        //Too many tries, remote host is overloaded or down
    } else {
        // Cannot perform CURL
    }
}
// Curl succeeded

此外,您可以查看CURLOPT_LOW_SPEED_LIMITCURLOPT_LOW_SPEED_TIME,以避免服务器中的任何开销。

答案 1 :(得分:0)

某事"低级"喜欢传递额外的标题?让s为部分下载文件的大小,只需使用Range: bytes=s-。 请参阅Requesting_a_specific_range_from_a_serverCURLOPT_HTTPHEADER explained

#include <curl/curl.h>
#include <string>
#include <sstream>


int dim=... //size of partial download sample1.bin
std::string s=std::to_string(dim);  // <-- here s is the string representing the size of the partial download
CURL *curl = curl_easy_init();
struct curl_slist *list = NULL;
if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL,"https://speed.hetzner.de/100MB.bin");
    list = curl_slist_append(list, "Range: bytes="+s+"-"); //from where it left off to the end (or where it stops again)
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
    curl_easy_perform(curl);
    curl_slist_free_all(list); /* free the list again */
}

还可以查看Making HTTPS GET with libcurl