用InternetOpen下载小文件 vc c++

文章改自:

http://hi.baidu.com/hacknothack/blog/item/4ca77710bc8e97fdc3ce79ad.html

使用vc6.0通过

 

#include "stdafx.h" #include<windows.h> #include<wininet.h> #include<iostream.h> #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://dt.163.com/images/news/0605/news02053101_5.jpg",NULL,0,INTERNET_FLAG_RELOAD,0); if (internetopenurl==NULL) { cout<<"Internet open url failed!"<<endl; goto there; } BOOL hwrite; DWORD written; HANDLE createfile; createfile=CreateFile("c://sss.jpg",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); if (createfile==INVALID_HANDLE_VALUE) { cout<<"Create File failed!"<<endl; goto next; } 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; goto here; } } cout<<"Finished downloading!"<<endl; here: CloseHandle(createfile); next: InternetCloseHandle(internetopenurl); there: InternetCloseHandle(internetopen); }

你可能感兴趣的:(C++,File,null,buffer,internet,testing)