使用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 }
