若是但愿获取下载或者上传进度相关信息,就给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);
设置一个回掉函数来获取数据git
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为例github
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);curl
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函数
---------------------
做者:小丑鱼_y
来源:CSDN
原文:https://blog.csdn.net/ixiaochouyu/article/details/47301005
版权声明:本文为博主原创文章,转载请附上博文连接!url