#include<windows.h> #include<wininet.h> #include<iostream> #include <tchar.h> using namespace std; #pragma comment(lib,"wininet.lib") void main() { DWORD byteread=0; char buffer[100]; memset(buffer,0,100); HINTERNET internetopen; internetopen = InternetOpen(/*_T("DownLoadFile")*/NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0); if (internetopen==NULL) { cout<<"Internet open failed!"<<endl; return; } HINTERNET internetopenurl; internetopenurl = InternetOpenUrl(internetopen,_T("http://topic.csdn.net/u/20090429/10/6a38f59f-b776-4140-a11b-f59c4a979931.html"), NULL,0,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_PRAGMA_NOCACHE,0); //下载的URL if (internetopenurl==NULL) { cout<<"Internet open url failed! error code = "<<GetLastError()<<endl; goto there; } BOOL hwrite; DWORD written; HANDLE createfile; createfile = CreateFile(_T("c://downloaded.html"),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); }