libcurl获取下载进度百分比,下载速度,剩余时间

如果希望获取下载或者上传进度相关信息,就给CURLOPT_NOPROGRESS属性设置0值

int ret = curl_easy_setopt(easy_handle, CURLOPT_URL, "http://speedtest.wdc01.softlayer.com/downloads/test10.zip");
ret |= curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, 0L);

设置一个回掉函数来获取数据

ret |= curl_easy_setopt(easy_handle, CURLOPT_XFERINFOFUNCTION, progress_callback);
progress_callback原型为
int progress_callback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
这个回调函数可以告诉我们有多少数据需要传输以及传输了多少数据,单位是字节。dltotal是需要下载的总字节数,dlnow是已经下载的字节数。ultotal是将要上传的字节数,ulnow是已经上传的字节数。如果你仅仅下载数据的话,那么ultotal,ulnow将会是0,反之,如果你仅仅上传的话,那么dltotal和dlnow也会是0。clientp为用户自定义参数,通过设置CURLOPT_XFERINFODATA属性来传递。此函数返回非0值将会中断传输,错误代码是 CURLE_ABORTED_BY_CALLBACK

传递用户自定义的参数,以CURL *easy_handle为例

ret |= curl_easy_setopt(easy_handle, CURLOPT_XFERINFODATA, easy_handle);
上面的下载可能有些问题,如果设置的URL不存在的话,服务器返回404错误,但是程序发现不了错误,还是会下载这个404页面。

这时需要设置CURLOPT_FAILONERROR属性,当HTTP返回值大于等于400的时候,请求失败

ret |= curl_easy_setopt(easy_handle, CURLOPT_FAILONERROR, 1L);

progress_callback的实现
int progress_callback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
	CURL *easy_handle = static_cast<CURL *>(clientp);
	char timeFormat[9] = "Unknow";

	// Defaults to bytes/second
	double speed;
	string unit = "B";

	curl_easy_getinfo(easy_handle, CURLINFO_SPEED_DOWNLOAD, &speed); // curl_get_info必须在curl_easy_perform之后调用

	if (speed != 0)
	{
		// Time remaining
		double leftTime = (downloadFileLength - dlnow - resumeByte) / speed;
		int hours = leftTime / 3600;
		int minutes = (leftTime - hours * 3600) / 60;
		int seconds = leftTime - hours * 3600 - minutes * 60;

#ifdef _WIN32
		sprintf_s(timeFormat, 9, "%02d:%02d:%02d", hours, minutes, seconds);
#else
		sprintf(timeFormat, "%02d:%02d:%02d", hours, minutes, seconds);
#endif
	}

	if (speed > 1024 * 1024 * 1024)
	{
		unit = "G";
		speed /= 1024 * 1024 * 1024;
	}
	else if (speed > 1024 * 1024)
	{
		unit = "M";
		speed /= 1024 * 1024;
	}
	else if (speed > 1024)
	{
		unit = "kB";
		speed /= 1024;
	}

	printf("speed:%.2f%s/s", speed, unit.c_str());

	if (dltotal != 0)
	{
		double progress = (dlnow + resumeByte) / downloadFileLength * 100;
		printf("\t%.2f%%\tRemaing time:%s\n", progress, timeFormat);
	}

	return 0;
}

整个工程的GitHub地址:https://github.com/forzxy/HttpClient


  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用libcurl库来实现下载进度的跟踪。下面是一个使用libcurl下载文件并显示进度的示例代码: ```c #include <stdio.h> #include <curl/curl.h> // 回调函数,用于跟踪下载进度 int progress_callback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) { // 计算下载进度百分比 double progress = dlnow / (double)dltotal * 100.0; printf("下载进度: %.2f%%\r", progress); fflush(stdout); // 刷新输出缓冲区 return 0; } int main() { CURL* curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if (curl) { // 设置下载地址 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/file_to_download"); // 设置进度回调函数 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); res = curl_easy_perform(curl); if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; } ``` 在上面的示例中,我们首先定义了一个名为`progress_callback`的回调函数,该函数在每个下载进度数据更新时被调用。然后,在主函数中,我们使用`curl_easy_setopt`函数设置了进度回调函数,并通过`curl_easy_perform`函数开始下载。 当你运行上述代码时,它将显示下载进度,类似于以下输出: ``` 下载进度: 50.00% ``` 你可以根据需要自定义进度显示的格式和其他操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值