查看单个帖子
旧 2008-06-04, 12:48 PM   #1
huangyhg
超级版主
 
huangyhg的头像
 
注册日期: 04-03
帖子: 18592
精华: 36
现金: 249466 标准币
资产: 1080358888 标准币
huangyhg 向着好的方向发展
默认 【转帖】使用windows api 从网上下载文件

在vckbase上看见的是采用mfc的代码,我就写了个api的,这个代码可以不用mfc



//首先加入所需的头文件

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <wininet.h>

//加入连接库
#pragma comment(lib, "wininet.lib")

int main(int argc, char* argv[])
{
printf("Hello World!
");
BYTE buf[1024];
TCHAR path[MAX_PATH];
DWORD num;


//打开
HINTERNET hinet = InternetOpen(_TEXT("Microsoft Internet Explorer"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_INVALID_PORT_NUMBER);

GetTempPath(MAX_PATH, path);
strcat(path, "\t.zip");

//获取文件句柄
HINTERNET hurl = InternetOpenUrl(hinet, _TEXT("http://file2.mydrivers.com/display/nvidia_forceware7645.zip"), NULL, 0, INTERNET_FLAG_NEED_FILE, NULL);

FILE* i = fopen(path, "wb");

//从internet下载文件内容
while(TRUE == InternetReadFile(hurl, buf, 1024, &num) && num>0)
{
fwrite(buf, 1, num, i);
}
fclose(i);

//使用完毕文件句柄和关闭连接
InternetCloseHandle(hurl);

InternetCloseHandle(hinet);


return 0;
}
huangyhg离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)