文章中有使用到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);//初始化libcurl
if (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;
}