vc下文件下载的两种方法
生活随笔
收集整理的這篇文章主要介紹了
vc下文件下载的两种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章中有使用到libcurl相關文件,請自行到官網下載編譯后導入使用:下面示例僅供參考
#include "stdafx.h" #include <stdio.h> #include <windows.h> #include <wininet.h> #include "curl/curl.h" #include "resource.h" #include <sys/stat.h> #include <fcntl.h> #include "HttpClient.h" #include <string> #pragma comment(lib, "Wininet.lib")#define MAXBLOCKSIZE 1024 //第一種方法:使用Internet系列函數下載 void download1(const char *url, const char* file) {HINTERNET hSession = InternetOpen("RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);if (hSession != NULL){HINTERNET handle2 = InternetOpenUrl(hSession, url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);if (handle2 != NULL){printf("%s\n", url);byte Temp[MAXBLOCKSIZE];ULONG Number = 1;FILE *stream;if ((stream = fopen(file, "wb")) != NULL){while (Number > 0){InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);fwrite(Temp, sizeof(char), Number, stream);}fclose(stream);}InternetCloseHandle(handle2);handle2 = NULL;}InternetCloseHandle(hSession);hSession = NULL;} }//第二種方法使用libcurl下載 size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {size_t written = fwrite(ptr, size, nmemb, stream);return written; }//進度條 int my_progress_func(char *progress_data,double total,double cur,double ultotal,double ulnow) {printf("%s (%.2lf%% -- cur=%.2lf)\n", progress_data, cur*100.0 / total, cur);return 0; }int download2(const char* url, const char outfilename[FILENAME_MAX]) {CURL *curl;FILE *fp;CURLcode res;char *progress_data = "* ";res = curl_global_init(CURL_GLOBAL_ALL);//初始化libcurlif (CURLE_OK != res){curl_global_cleanup();return -1;}curl = curl_easy_init();//得到 easy interface型指針if (curl) {fopen_s(&fp, outfilename, "wb");curl_easy_setopt(curl, CURLOPT_URL, url);/* 調用curl_easy_setopt()設置傳輸選項 */curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);/* 根據curl_easy_setopt()設置的傳輸選項,實現回調函數以完成用戶特定任務 */curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);//開啟進度回調功能curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);//設置進度回調函數 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, progress_data);//設置進度回調函數參數curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);res = curl_easy_perform(curl); // 調用curl_easy_perform()函數完成傳輸任務 fclose(fp);if (res != CURLE_OK) {fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));curl_easy_cleanup(curl);return -1;}curl_easy_cleanup(curl); // 調用curl_easy_cleanup()釋放內存 }curl_global_cleanup();return 0; }int _tmain(int argc, _TCHAR* argv[]) {download1("http://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=http%3A%2F%2Fpic49.nipic.com%2Ffile%2F20140927%2F19617624_230415502002_2.jpg&thumburl=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D2611079001%2C3896435225%26fm%3D26%26gp%3D0.jpg", "C:\\download1.jpg");download2("http://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=http%3A%2F%2Fpic49.nipic.com%2Ffile%2F20140927%2F19617624_230415502002_2.jpg&thumburl=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D2611079001%2C3896435225%26fm%3D26%26gp%3D0.jpg", "C:\\download2.jpg");while (1);return 0; }總結
以上是生活随笔為你收集整理的vc下文件下载的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android实现视频剪切、视频拼接以及
- 下一篇: 网址导航站