简体   繁体   English

libcurl错误,curl_easy_perform()失败:c ++

[英]libcurl error, curl_easy_perform() failed: c++

I have written this code for downloading a file using libcurl: 我已经编写了使用libcurl下载文件的代码:

#ifndef CHECK_RESOURCES_H
#define CHECK_RESOURCES_H

#include <string>
#include <stdio.h>
#include <curl/curl.h>
// #include <bzlib.h>


void progress_bar (void* ptr, double TotalToDownload, double NowDownloaded, \
    double TotalToUpload, double NowUploaded) {

    printf("%f : %f \n", TotalToDownload, NowDownloaded);
}

void download_file () {
    CURL *curl;
    CURLcode res;
    char outFileName[FILENAME_MAX] = "shape_predictor_68_face_landmarks.dat.bz2";
    std::string url = "http://ufpr.dl.sourceforge.net/project/dclib/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2";
    FILE *fp;

    curl = curl_easy_init ();
    if (curl) {
        fp = fopen (outFileName, "wb");

        curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

        // Internal CURL progressmeter must be disabled if we provide our own callback
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
        // Install the callback function
        curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_bar);

        res = curl_easy_perform (curl);

        if (res != CURLE_OK) {
            fprintf(stderr, "%s%s\n", "curl_easy_perform() failed : ", curl_easy_strerror(res));
        }

        curl_easy_cleanup (curl);
        fclose (fp);
    }
}

void extract_bz2 () {

}


void check_dlib_landmark_weights (const std::string& name) {
    if (FILE *file = fopen(name.c_str(), "r")) {
        fclose(file);
    } else {
        download_file ();
    }
}


#endif  // CHECK_RESOURCES_H

The problem is that when I run the code it gives this output: 问题是,当我运行代码时,它给出以下输出:

0.000000 : 0.000000 
0.000000 : 0.000000 
curl_easy_perform() failed : Operation was aborted by an application callback

I dont understand why this is failing. 我不明白为什么这失败了。 I have checked the link I am using and it's a working link (downloaded the file without any prob). 我检查了我正在使用的链接,它是一个有效的链接(下载文件时没有任何问题)。 How do I correct this? 我该如何纠正?

The CURLOPT_PROGRESSFUNCTION expects that the callback function will return a value of 0, currently your function returns void. CURLOPT_PROGRESSFUNCTION期望回调函数将返回值0,当前您的函数返回void。 If you modify your progress_bar function to return int instead of void and add return 0; 如果您修改progress_bar函数以返回int而不是void并添加return 0; to the end of it that should resolve the issue. 到最后应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM