httpdownload小程序 internetread的应用
2014-01-12 22:48
225 查看
此文本人QQ空间所写文章;纯代码级的。没有多少注释。
此文是利用InternetOpen(),InternetOpenUrl(),InternetReadFile(),这几个网络相关的读写函数来达到下载文件的功能。当然,这里的文件必须是直接连接能够给出地址而访问到的资源;而不是像迅雷资源那样需要迅雷解析的那种url。
文章还涉及到字节编码,比如单字节还是双字节问题;
具体看代码;
封装一个Internet类 ,最好不要用这个类名,这个有可能会和标准库中有重名问题;这里只是测试;
封装一个文件下载类,主要是用来存从Internet下载下来的文件
主文件
此文是利用InternetOpen(),InternetOpenUrl(),InternetReadFile(),这几个网络相关的读写函数来达到下载文件的功能。当然,这里的文件必须是直接连接能够给出地址而访问到的资源;而不是像迅雷资源那样需要迅雷解析的那种url。
文章还涉及到字节编码,比如单字节还是双字节问题;
具体看代码;
封装一个Internet类 ,最好不要用这个类名,这个有可能会和标准库中有重名问题;这里只是测试;
/////////////class My_Internet.h/////////////// #pragma comment(lib,"wininet.lib") #pragma once ///////////////////////////////////////// #include"windows.h" #include"wininet.h" #include"Tchar.h" class Internet { public: Internet(LPCWSTR Download_Address) { Internetopen=InternetOpen(_T("CQ"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0); Internetopenurl=InternetOpenUrl(Internetopen,Download_Address,NULL,0,INTERNET_FLAG_PRAGMA_NOCACHE,0); } ~Internet() { InternetCloseHandle(InternetOpen); InternetCloseHandle(InternetOpenUrl); } public: HINTERNET Internetopen; HINTERNET Internetopenurl; DWORD byteread; public: int Get_FileFromUrl(char *Buffer,int length) { InternetReadFile(Internetopenurl,Buffer,length,&byteread); return byteread; } };
封装一个文件下载类,主要是用来存从Internet下载下来的文件
//////////////class of My_downFile.h///////////// #include"windows.h" #include"stdio.h" #include"Tchar.h" /////////////head file///////////////////////// class My_DownFile{ public: My_DownFile( char * Destination) //构造函数, 以目的路径作为参数 { memset(Buffer,0,100); Createfile=CreateFile((LPCWSTR)(Destination),GENERIC_WRITE,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); } ~My_DownFile() { CloseHandle(Createfile); } public: char Buffer[100]; //接受buffer大小 BOOL hwrite; DWORD written; HANDLE Createfile; public: int WriteFileToPC() //文件写入的本地 { return WriteFile(Createfile,Buffer,sizeof(Buffer),&written,NULL); } DWORD Buffersize() //返回buffer大小 { hwrite =sizeof(Buffer); return hwrite; } };
主文件
//////////////////////////////main//////////////////////// #include"iostream" #include"My_Internet.h" //引入封装的网络类 #include"My_DownFile.h" //引入封装的文件下载保存类 using namespace std; //////////////////main//////////////// void main() { WCHAR Downloadaddress[]=_T("http://www.softbar.com/iptoolimage/data.jpg"); //要下载的目的地址。 //WCHAR *Downloadaddress=new WCHAR[strlen(C_Downloadaddress)]; //swprintf(Downloadaddress,L"%s",C_Downloadaddress); Internet Download(Downloadaddress); //出现一个问题,测试发现文件确实和c实现的一样下载下来了,可是不会在D盘里保存. //问题出在这里.原来是文件创建这里文件名是单字节的,而createfime却要宽字节。修改了 WCHAR FileName[]=_T("D://CHENQIANG.jpg"); //保存的目的地址 My_DownFile Downloadfile(FileName); ////////////for the test of createfile//////////////////////// // CreateFile(_T("d://1.txt"),GENERIC_WRITE,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); ///////////////////////////////////////////////////////////// if(Download. Internetopen==NULL){ cout<<"open the internet failed"<<endl; return ; } if(Download. Internetopenurl==NULL){ cout<<"open the internet url failed"<<endl; return ; } if(Downloadfile.Createfile ==INVALID_HANDLE_VALUE){ cout<<"create the file failed"<<endl; return; } int T_num=0; while(1){ if(Download.Get_FileFromUrl(Downloadfile.Buffer,Downloadfile.Buffersize ())==0) break; if(Downloadfile.WriteFileToPC ()==0){ cout<<"write file failed "<<endl; } T_num++; //for the test } cout<<"total is "<<T_num<<endl; system("pause"); //delete Downloadaddress; }
相关文章推荐
- socket上http协议应用(使用socket进行http通信的例子,准备好报头以后,简单read/write就可以了)
- 一般处理程序HttpHandler的应用
- 修改http请求文件为本地文件的一种方法:hook InternetReadFile 和 HttpOpenRequest
- linux网络编程(5)——应用层网络服务程序_HTTP#FTP#TELNET#NFS
- 采用MASM32编写的查看网页源代码程序——HttpRead,文件大小仅为 5632 字节
- eclipse中 Unable to read repository at http://download.eclipse.org/releases/indigo 解决方案
- internetopen、internetOpenUrl、InternetReadFile,urldownloadtofile
- asp无组件上传类的应用实例/化境HTTP上传程序
- ubuntu 自添加开机启动程序(一种使ethtool设置永久保存在网络设备中的曲线救国方法) 转自:http://www.groad.net/bbs/read.php?tid-1393.html
- GDI+ 在Delphi程序的应用 -- ColorMatrix与图像灰度化
- iPhone企业应用实例分析之三:程序框架分析
- 应用 Valgrind 发现 Linux 程序的内存问题
- 一个应用二叉树基本算法的程序
- Android应用实例之----天气预报程序.
- IIS 7.5 HTTP 错误 404.17 – Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。
- Linux笔记 6 (自举程序GRUB配置与应用)
- ABAP动态生成经典应用之Dynamic SQL Excute 程序
- Excel VBA高效办公应用-第十一章-教师员工数据管理-Part2 (课表助手小程序)
- HttpWebRequest 和 HttpWebResponse 的应用
- JAVA基础应用——执行外部程序(Apache Commons Exec)