使用libcurl下载https地址的文件

使用libcurl下载https地址的文件

 1 void downLoadFile(std::string filename, std::string newFilename)
 2 {
 3     CURL *curl_handle;
 4     static const char *pagefilename = (char *)newFilename.data();
 5     FILE *pagefile;
 6     char *p = (char *)filename.data();
 7     curl_global_init(CURL_GLOBAL_ALL);
 8 
 9     /* init the curl session */
10     curl_handle = curl_easy_init();
11 
12     /* set URL to get here */
13     curl_easy_setopt(curl_handle, CURLOPT_URL, p);
14 
15     /* Switch on full protocol/debug output while testing */
16     curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
17 
18     /* disable progress meter, set to 0L to enable and disable debug output */
19     curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
20     /* google.com is redirected, so we tell LibCurl to follow redirection */
21     curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
22     /* SSL Options */
23     curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 1);
24     curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 1);
25 
26     /* Provide CA Certs from http://curl.haxx.se/docs/caextract.html */
27     curl_easy_setopt(curl_handle, CURLOPT_CAINFO, "ca-bundle.crt");
28     /* send all data to this function  */
29     curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
30 
31     /* open the file */
32     pagefile = fopen(pagefilename, "wb");
33     if (pagefile) {
34 
35         /* write the page body to this file handle */
36         curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
37 
38         /* get it! */
39         curl_easy_perform(curl_handle);
40 
41         /* close the header file */
42         fclose(pagefile);
43     }
44 
45     /* cleanup curl stuff */
46     curl_easy_cleanup(curl_handle);
47 
48     return ;
49 }
1 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
2 {
3     size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
4     return written;
5 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值