码迷,mamicode.com
首页 > Web开发 > 详细

用InternetOpen()的下载文件

时间:2014-08-07 15:52:10      阅读:686      评论:0      收藏:0      [点我收藏+]

标签:c++   windows   

#include<windows.h>
#include<wininet.h>
#include<iostream>
using namespace std;
#pragma comment(lib,"wininet.lib")
void main()
{
	DWORD byteread=0;
	char buffer[100];
	memset(buffer,0,100);
	HINTERNET internetopen;
	internetopen=InternetOpen("Testing",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
	if (internetopen==NULL){
		cout<<"Internet open failed!"<<endl;
		return;}
	HINTERNET internetopenurl;
	internetopenurl=InternetOpenUrl(internetopen,"http://package6.kuaiapp.cn/201407/26/6002_887171753_1.0.0_5.0.ipa",NULL,0,INTERNET_FLAG_RELOAD,0);
	if (internetopenurl==NULL){
		cout<<"Internet open url failed!"<<endl;
		 goto there;}
	BOOL hwrite = 0;
	DWORD written = 0;
	HANDLE createfile;
	createfile=CreateFile("c:\\sss.exe",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
	if (createfile==INVALID_HANDLE_VALUE){
		cout<<"Create File failed!"<<endl;
		goto next;}
	BOOL internetreadfile = 0;
	while(1){
		internetreadfile=InternetReadFile(internetopenurl,buffer,sizeof(buffer),&byteread);
		if(byteread==0)
			break;
		hwrite=WriteFile(createfile,buffer,sizeof(buffer),&written,NULL);
		if (hwrite==0){
			cout<<"Write to file failed!"<<endl;
			   goto here;}
	}
	cout<<"Finished downloading!"<<endl;
here:
	CloseHandle(createfile);
next: 
	InternetCloseHandle(internetopenurl);
there:
	InternetCloseHandle(internetopen);
}



之后研究研究断点传输

用InternetOpen()的下载文件,布布扣,bubuko.com

用InternetOpen()的下载文件

标签:c++   windows   

原文地址:http://blog.csdn.net/heaven13483/article/details/38417957

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!