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/zyumle/HttpClient


版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(http,get,curl,libcurl,下载进度)