libcurl 通過http協議下載文件並顯示下載進度


vc6 測試工程下載地址: http://download.csdn.net/detail/mtour/8068053

 

代碼如下:

 

 

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)  
  2. {  
  3.   return fwrite(ptr, size, nmemb, stream);  
  4. }   
  5.    
  6. int my_progress_func(char *progress_data,  
  7.                      double t, /* dltotal */  
  8.                      double d, /* dlnow */  
  9.                      double ultotal,  
  10.                      double ulnow)  
  11. {  
  12.   printf("%s %g / %g (%g %%)\n", progress_data, d, t, d*100.0/t);  
  13.   return 0;  
  14. }  
  15.    
  16. int main(int argc, char **argv)  
  17. {  
  18.   CURL *curl;  
  19.   CURLcode res;  
  20.   FILE *outfile;  
  21.   char *url = "http://10.10.1.4/d/c00000000000039/2014-10-22/10-28-35.ps";  
  22.   char *progress_data = "* ";  
  23.    
  24.   curl = curl_easy_init();  
  25.   if(curl)  
  26.   {  
  27.     outfile = fopen("test.ps", "wb");  
  28.    
  29.     curl_easy_setopt(curl, CURLOPT_URL, url);  
  30.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);  
  31.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);  
  32.     curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);  
  33.     curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);  
  34.     curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, progress_data);  
  35.    
  36.     res = curl_easy_perform(curl);  
  37.    
  38.     fclose(outfile);  
  39.     /* always cleanup */  
  40.     curl_easy_cleanup(curl);  
  41.   }  
  42.   return 0;  
  43. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM