如何在libcurl中获取下载总时间?

gcxthw6b  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(73)

在xferinfo2函数中,返回ret

errcord [48]. 48 is CURLE_UNKNOWN_OPTION, /* 48 - User specified an unknown option */

字符串
和printf结果为

ret 48
TOTAL TIME: 0.000000
DOWN: 41992 of 45998104


curl_easy_getinfo函数中有什么问题的??
这个curl_easy_getinfo (curl,CURLINFO_TOTAL_TIME, &curtime)在POST方法中工作正常。但是,GET方法有一个问题。

#include "curl.h"
#include "json.h"
#include <fcntl.h>

typedef struct myprogress 
{
  curl_off_t lastruntime; /* type depends on version, see above */
  CURL *curl;
}myprogress;

static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) 
{
    size_t written = fwrite(ptr, size, nmemb, stream);
    return written;
}

static int xferinfo(void *p,
                    curl_off_t dltotal, curl_off_t dlnow,
                    curl_off_t ultotal, curl_off_t ulnow)
{
    struct myprogress *myp = (struct myprogress *)p;

    CURL *curl = myp->curl;
    double curtime = 0;

    int ret = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &curtime);  // <<< HERE
    printf("ret %d\n", ret);   // <<< HERE

    myp->lastruntime = curtime;

    fprintf(stderr, "TOTAL TIME: %f \r\n", curtime);

    fprintf(stderr,"  DOWN: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
                    "\r\n", dlnow, dltotal);

    if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
        return 1;
    return 0;
}

CURLcode curl_get(const char* Path, const char* URL)
{
    CURL *curl;
    FILE *fp;
    CURLcode res = 0;
    struct myprogress prog;

    fp = fopen(Path, "wb");
    if(!fp)
    {
        LOGE("post text file fail\n"); /* cannot continue */
        return FAILURE;
    }

    curl = curl_easy_init();
    if (curl)
    {
        //prog.lastruntime = 0;
        //prog.curl = curl;
        
        curl_easy_setopt(curl, CURLOPT_URL, URL);
        
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);

        /* Install the callback function */
        curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, **xferinfo**);

        /* pass the struct pointer into the xferinfo function */
        curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
        
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        
        res = curl_easy_perform(curl);
        if(res != CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() %d failed: %s\n", __LINE__, curl_easy_strerror(res));
            res = FAILURE;
        }

        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    else
    {
        res = CURLE_FAILED_INIT;
        fprintf(stderr, "curl_easy_perform() %d failed: %s\n", __LINE__, curl_easy_strerror(res));
        res = FAILURE;
    }
    
    fclose(fp);
    
    return res;
}

wfveoks0

wfveoks01#

现在我修复了它。问题是prog。在回调函数(xferinfo)之前,它必须像这样初始化prog

prog.lastruntime = 0;
prog.curl = curl;

字符串
这一点很重要。

curl = curl_easy_init();
    if (curl)
    {
        prog.lastruntime = 0;   // <<<< HERE
        prog.curl = curl;       // <<<< HERE
        
        curl_easy_setopt(curl, CURLOPT_URL, URL);
        
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
        /* Install the callback function */
        curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo2);
        /* pass the struct pointer into the xferinfo function */
        curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
        
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        
        res = curl_easy_perform(curl);
        if(res != CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() %d failed: %s\n", __LINE__, curl_easy_strerror(res));
            res = FAILURE;
        }

        /* always cleanup */
        curl_easy_cleanup(curl);
    }


谢谢大家。

相关问题