您的位置:首页 > 理论基础 > 计算机网络

httpdownload小程序 internetread的应用

2014-01-12 22:48 225 查看
此文本人QQ空间所写文章;纯代码级的。没有多少注释。

此文是利用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐