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

本文介绍如何使用CURL库设置属性以获取下载和上传的进度信息,并在遇到HTTP错误时进行错误处理。包括设置回调函数、进度显示、错误检查等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果希望获取下载或者上传进度相关信息,就给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


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值