用WinInet远程下载文件的示例代码
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#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); } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=1133