使用libcurl下载https地址的文件

时间:2023-03-09 05:30:01
使用libcurl下载https地址的文件

使用libcurl下载https地址的文件

 void downLoadFile(std::string filename, std::string newFilename)
{
CURL *curl_handle;
static const char *pagefilename = (char *)newFilename.data();
FILE *pagefile;
char *p = (char *)filename.data();
curl_global_init(CURL_GLOBAL_ALL); /* init the curl session */
curl_handle = curl_easy_init(); /* set URL to get here */
curl_easy_setopt(curl_handle, CURLOPT_URL, p); /* Switch on full protocol/debug output while testing */
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); /* disable progress meter, set to 0L to enable and disable debug output */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
/* google.com is redirected, so we tell LibCurl to follow redirection */
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
/* SSL Options */
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, );
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, ); /* Provide CA Certs from http://curl.haxx.se/docs/caextract.html */
curl_easy_setopt(curl_handle, CURLOPT_CAINFO, "ca-bundle.crt");
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); /* open the file */
pagefile = fopen(pagefilename, "wb");
if (pagefile) { /* write the page body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile); /* get it! */
curl_easy_perform(curl_handle); /* close the header file */
fclose(pagefile);
} /* cleanup curl stuff */
curl_easy_cleanup(curl_handle); return ;
}
 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}

使用libcurl下载https地址的文件

下载https地址的qq.exe程序

如果有朋友不想编译,我也把我编好的库和lib头文件打包,https://files.cnblogs.com/files/nightnine/libcurl.zip

相关文章