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

问题描述 投票:0回答:1

在xferinfo2函数中 ret 返回 errcord [48]。 48 是 CURLE_UNKNOWN_OPTION, /* 48 - 用户指定了未知选项 */

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);

    printf("ret %d\n", ret);**

    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;
}
c libcurl
1个回答
0
投票

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

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

这一点很重要。

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, 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);
    }

谢谢大家。

© www.soinside.com 2019 - 2024. All rights reserved.