使用libcurl

时间:2016-03-16 15:34:15

标签: c++ libcurl

我正在尝试从网站下载大量文件。我递归地从一个页面到另一个页面并从那里提取链接。我下载后 543个文件(我已尝试过几次,但数字总是一样的), 我收到了错误代码2,它引用了CURLE_FAILED_INIT。

一段代码,它下载了一个文件(从本教程之一复制粘贴):

CURLcode curl_read(const std::string& url, std::string& buffer, long timeout = 30) {
    CURLcode code(CURLE_FAILED_INIT);
    CURL* curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

    if(curl) {
        if(CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_URL, url.c_str()))
        && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, string_write))
        && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer))
        && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L))
        && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L))
        && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout))) {

          code = curl_easy_perform(curl);
        }
        else {
            std::cout << "Error inside setting curl" << std::endl;
        }
        curl_easy_cleanup(curl);
    }
    else {
        std::cout << "Error inside curl_easy_init" << std::endl;
    }
    return code;
}

在此代码运行之前,我调用了

curl_global_init(CURL_GLOBAL_ALL);

0 个答案:

没有答案