- UID
- 77
- 精华
- 积分
- 9576
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
AA55写过一个纯SOCKET实现文件下载的代码:http://www.0xaa55.com/thread-1093-1-1.html
然后我忽然想起来了这一份代码也可以实现文件下载。这份代码的代码量少,而且没有用到那个在黑名单里的UrlDownloadToFile。
- #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);
- }
复制代码 PS:本代码的原出处已不可考。。。 |
|