#ifndef _HTTP_DOWN_LOAD_FILE_H
#define _HTTP_DOWN_LOAD_FILE_H
#include <windows.h>
#include <wininet.h>
#pragma comment(lib,”wininet.lib”)
#pragma comment(lib,”Winmm.lib”)
class CHttpDownLoadFile
{
public:
enum DownLoadMode //下载模式
{
Mode_BlockNoMsg = 1, //阻塞,不传递消息模式
Mode_BlockAndMsg, //阻塞,传递消息模式
Mode_UnBlockNoMsg, //非阻塞,不传递消息模式
Mode_UnBlockAndMsg, //非阻塞,传递消息模式
};
enum DownLoadMsg //下载时发送的消息
{
DL_OK = 0x0001,
DL_ERROR,
DL_START,
DL_FILESIZE,
DL_READDATA,
DL_TERMINATE,
};
enum DownLoadWndMsg //定义的windows消息
{
DL_MSG = WM_USER + 200,
};
private:
HWND m_hWnd; // 下载时,消息的接收窗体
int m_DownLoadMode; // 下载模式
BOOL m_OverWrite; // 是否覆盖
int m_ErrorCode; // 错误码
DWORD m_Speed; // 下载速度
DWORD m_ReadCount; // 已经下载的大小
DWORD m_FileSize; // 文件大小
DWORD m_MsgCode; // 下载时,发送的消息代码,初始化时等于 DL_MSG
TCHAR m_OutputFileName[_MAX_PATH]; // 保存下载时的文件名
TCHAR m_URL[_MAX_PATH]; // 资源的URL
BOOL m_bDownLoadWebOK; // 是否下载完成
DWORD m_WaitTime; // 下载的时间
private:
HINTERNET FhSession ; //会话句柄
HINTERNET FhConnect ; //http连接句柄
HINTERNET FhRequest ; //http请求句柄
HANDLE hFile;
public:
CHttpDownLoadFile();
CHttpDownLoadFile(HWND hWnd);
~CHttpDownLoadFile();
public:
LPSTR GetURL() { return m_URL; }
void SetURL( LPSTR Value ) { strcpy(m_URL,Value); }
LPSTR GetOutputFileName() { return m_OutputFileName; }
void SetOutputFileName( LPSTR fn ) { strcpy(m_OutputFileName, fn); }
BOOL SetOverWrite() { return m_OverWrite; }
void SetOverWrite(BOOL Value) { m_OverWrite = Value; }
void SetWndHandle(HWND hWnd) { m_hWnd = hWnd; }
HWND GetWndHandle() { return m_hWnd; }
void SetDownLoadMode(int mode) { m_DownLoadMode= mode; }
int GetDownLoadMode() { return m_DownLoadMode; }
void SetMsgCode(DWORD code) { m_MsgCode = code; }
DWORD GetSpeed() { return m_Speed; }
DWORD GetFileSize() { return m_FileSize; }
DWORD GetBeenDownLoadedSize() { return m_ReadCount; }
public:
int StartDownLoad(); // 开始下载,并把下载的内容保存到文件里
public:
int _RealDownLoad(); // 实际下载
void ParseURL(char *Url,char *pHostName,char *pFileName,DWORD* nPort);
public:
static int StartDownLoad(LPSTR url,
LPSTR outputfile,
int mode = Mode_BlockNoMsg,
HWND hWnd = NULL,
BOOL isOverWrite = FALSE);
static ULONG WINAPI DownLoadProc(LPVOID pDownLoadFile);
private:
void DisposeError();
};
#endif
//HttpDownLoadFile.cpp
#include “HttpDownLoadFile.h”
CHttpDownLoadFile::CHttpDownLoadFile()
{
m_hWnd = NULL;
m_DownLoadMode = Mode_BlockNoMsg;
m_OverWrite = TRUE;
m_bDownLoadWebOK = FALSE;
m_ErrorCode = 0;
m_Speed = 0;
m_ReadCount = 0;
m_FileSize = 0;
m_MsgCode = DL_MSG;
m_WaitTime = 10000;
hFile = NULL;
memset(m_URL,0x00,sizeof(m_URL));
memset(m_URL,0x00,sizeof(m_OutputFileName));
}
CHttpDownLoadFile::CHttpDownLoadFile(HWND hWnd)
{
m_hWnd = hWnd;
m_DownLoadMode = Mode_BlockNoMsg;
m_OverWrite = TRUE;
m_bDownLoadWebOK = FALSE;
m_ErrorCode = 0;
m_Speed = 0;
m_ReadCount = 0;
m_FileSize = 0;
m_MsgCode = DL_MSG;
hFile = NULL;
memset(m_URL,0x00,sizeof(m_URL));
memset(m_URL,0x00,sizeof(m_OutputFileName));
}
int CHttpDownLoadFile::StartDownLoad()
{
if( m_DownLoadMode == Mode_BlockNoMsg || m_DownLoadMode == Mode_BlockAndMsg )
{
return this->_RealDownLoad();
}else
{
ULONG threadID;
HANDLE hThread = CreateThread(NULL,0,DownLoadProc,this,0,&threadID);
CloseHandle(hThread);
return TRUE;
}
}
ULONG WINAPI CHttpDownLoadFile::DownLoadProc(LPVOID pDownLoadFile)
{
CHttpDownLoadFile* pDownLoad = (CHttpDownLoadFile*)(pDownLoadFile);
return pDownLoad->_RealDownLoad();
}
int CHttpDownLoadFile::_RealDownLoad()
{
// 发送开始传输消息
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
::SendMessage(m_hWnd,m_MsgCode,DL_START,0);
}
// 打开链接,开始下载
FhSession = ::InternetOpen(“HttpDownLoadFile”, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);// 同步方式
if(FhSession == NULL || FhSession == INVALID_HANDLE_VALUE)
{
//出错处理
this->DisposeError();
return 0;
}
TCHAR server[_MAX_PATH];
TCHAR fileName[_MAX_PATH];
DWORD nPort = INTERNET_DEFAULT_HTTP_PORT;
memset(server,0x00,_MAX_PATH);
memset(fileName,0x00,_MAX_PATH);
const char *FAcceptTypes[] = {“*/*”,NULL};
this->ParseURL(m_URL,server,fileName,&nPort);
// 得到下载后的文件名
if( strcmp(m_OutputFileName,””) == 0)
{
strcpy(m_OutputFileName,fileName);
}
FhConnect = ::InternetConnect(FhSession, server, nPort,
NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if(FhConnect==NULL || FhConnect == INVALID_HANDLE_VALUE)
{
//出错处理
this->DisposeError();
return 0;
}
FhRequest = ::HttpOpenRequest(FhConnect, “GET”, fileName, “HTTP/1.0”, NULL, FAcceptTypes,
INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 0);
if(FhRequest==NULL || FhRequest == INVALID_HANDLE_VALUE)
{
//出错处理
this->DisposeError();
return 0;
}
const char http_accept_header[] = “Accept: */*/r/n”;
if (!::HttpAddRequestHeaders(FhRequest, http_accept_header, strlen(http_accept_header),
HTTP_ADDREQ_FLAG_REPLACE))
{
//出错处理
this->DisposeError();
return 0;
}
const char data_type_header[] = “Content-Type: application/x-www-form-urlencoded/r/n”;
if ( HttpAddRequestHeaders(FhRequest,data_type_header,strlen(data_type_header),
HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE) == FALSE )
{
this->DisposeError();
return 0;
}
if(::HttpSendRequest(FhRequest, NULL, 0, NULL, 0)==FALSE)
{
//出错处理
this->DisposeError();
return 0;
}
// 发送文件大小消息
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
char TmpBuf[64] = “/0″;
DWORD bufLength = 64;
memset(TmpBuf,0x00,64);
if(::HttpQueryInfo(FhRequest, HTTP_QUERY_CONTENT_LENGTH, TmpBuf,&bufLength, NULL)==FALSE)
{
::SendMessage(m_hWnd, DL_MSG, DL_ERROR, 0);
}
else
{
this->m_FileSize = atoi(TmpBuf);
::SendMessage(m_hWnd, DL_MSG, DL_FILESIZE, m_FileSize);
}
}
// 创建文件
hFile = CreateFile(m_OutputFileName,GENERIC_WRITE,FILE_SHARE_READ,NULL, /
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if( hFile == INVALID_HANDLE_VALUE )
{
return FALSE;
}
const int bufLength = 4096;
char buf[bufLength];
DWORD realWrite = 0;
DWORD dwRead = 0;
DWORD bRet = 0;
memset(buf,0,bufLength);
DWORD tick = ::timeGetTime();
while(1)
{
bRet = ::InternetReadFile(FhRequest, (LPVOID)(buf), bufLength, &dwRead);
if(dwRead<=0) break; //已读到文件尾
if(bRet==FALSE)
{
this->DisposeError();
return 0;
}
m_ReadCount = m_ReadCount+dwRead;
if(::WriteFile(hFile,(void *)buf,dwRead,&realWrite,(LPOVERLAPPED)NULL)==0)
{//写操作出错处理
this->DisposeError();
return 0;
}
if(::timeGetTime()-tick > 0)
{
m_Speed=(m_ReadCount/(::timeGetTime()-tick))*1000;
}
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
::SendMessage((HWND)m_hWnd, DL_MSG, DL_READDATA, (LPARAM)this);
}
}
if(hFile)
{
::CloseHandle(hFile);
hFile = NULL;
}
if(FhRequest) {
::InternetCloseHandle(FhRequest);
FhRequest = NULL;
}
if(FhConnect) {
::InternetCloseHandle(FhConnect);
FhConnect = NULL;
}
if(FhSession) {
::InternetCloseHandle(FhSession);
FhSession = NULL;
}
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
::SendMessage(m_hWnd,m_MsgCode,DL_TERMINATE,0);
}
return TRUE;
}
int CHttpDownLoadFile::StartDownLoad(LPSTR url,
LPSTR outputfile,
int mode,
HWND hWnd,
BOOL isOverWrite)
{
CHttpDownLoadFile* pDownLoad = new CHttpDownLoadFile;
pDownLoad->SetWndHandle(hWnd);
pDownLoad->SetOverWrite(isOverWrite);
pDownLoad->SetOutputFileName(outputfile);
pDownLoad->SetURL(url);
pDownLoad->SetDownLoadMode(mode);
return pDownLoad->StartDownLoad();
}
void CHttpDownLoadFile::ParseURL(char *Url,char *pHostName,char *pFileName,DWORD* nPort)
{
char *all=Url;
char *p=strstr(all,”http://”);
char Buffer[_MAX_PATH];
memset(Buffer,0x00,_MAX_PATH);
if(p)
{
int pos=p-all;
all=all+pos+strlen(“http://”);
p=strstr(all,”/”);
if(p)
{
pos=p-all;
memcpy(Buffer,all,pos);
strcpy(pFileName,all+pos);
p = strstr(Buffer,”:”);
if(p)
{
pos = p – Buffer;
memcpy(pHostName,Buffer,pos);
*nPort = atoi(p+1);
}else
{
strcpy(pHostName,Buffer);
}
}
}
}
void CHttpDownLoadFile::DisposeError()
{
if(FhRequest) {
::InternetCloseHandle(FhRequest);
FhRequest = NULL;
}
if(FhConnect) {
::InternetCloseHandle(FhConnect);
FhConnect = NULL;
}
if(FhSession) {
::InternetCloseHandle(FhSession);
FhSession = NULL;
}
if(hFile) {
::CloseHandle(hFile);
hFile = NULL;
}
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
::SendMessage(m_hWnd,m_MsgCode,DL_ERROR,0);
}
}
//main.cpp 测试
int main(int argc,char* argv[])
{
CHttpDownLoadFile* pDown = new CHttpDownLoadFile();
pDown->SetDownLoadMode(CHttpDownLoadFile::DownLoadMode::Mode_BlockNoMsg);
pDown->SetURL(“http://www.brstime.com/”);
pDown->SetOutputFileName(“brstime.html”);
if( pDown->StartDownLoad() == TRUE)
{
printf(“down load ok!”);
}
return 1;
}
/*———————————
CHttpDownLoadFile 的功能就是把网络上的网页,文件(测试过文本文件),下载到本地,并保存成文件。这个类的效率比较低,适用于下载比较小的文件,且不要求速度的场合。
CHttpDownLoadFile 提供4中下载模式,即下载时是否发送消息,是否以阻塞的方式下载等。
测试用例中的下载模式是以阻塞且不发送消息的下载模式、下载一个网页。
*/
在上文的基础上,增加了保存模式功能,即提供把文件内容保存到string对象里,而不是写到文件。
#ifndef _HTTP_DOWN_LOAD_FILE_H
#define _HTTP_DOWN_LOAD_FILE_H
#include <windows.h>
#include <wininet.h>
#include <string>
using std::string;
#pragma comment(lib,”wininet.lib”)
#pragma comment(lib,”Winmm.lib”)
class CHttpDownLoadFile
{
public:
enum DownLoadMode //下载模式
{
Mode_BlockNoMsg = 1, //阻塞,不传递消息模式
Mode_BlockAndMsg, //阻塞,传递消息模式
Mode_UnBlockNoMsg, //非阻塞,不传递消息模式
Mode_UnBlockAndMsg, //非阻塞,传递消息模式
};
enum DownLoadMsg //下载时发送的消息
{
DL_OK = 0x0001,
DL_ERROR,
DL_START,
DL_FILESIZE,
DL_READDATA,
DL_TERMINATE,
};
enum DownLoadWndMsg //定义的windows消息
{
DL_MSG = WM_USER + 200,
};
enum ESaveMode //保存模式
{
Mode_SaveToFile = 1, //保存到文件
Mode_SaveToBuffer, //保存到缓冲区
};
private:
HWND m_hWnd; // 下载时,消息的接收窗体
int m_DownLoadMode; // 下载模式
BOOL m_OverWrite; // 是否覆盖
int m_ErrorCode; // 错误码
DWORD m_Speed; // 下载速度
DWORD m_ReadCount; // 已经下载的大小
DWORD m_FileSize; // 文件大小
DWORD m_MsgCode; // 下载时,发送的消息代码,初始化时等于 DL_MSG
TCHAR m_OutputFileName[_MAX_PATH]; // 保存下载时的文件名
TCHAR m_URL[_MAX_PATH]; // 资源的URL
BOOL m_bDownLoadWebOK; // 是否下载完成
DWORD m_WaitTime; // 下载的时间
int m_SaveMode; // 保存模式
private:
HINTERNET FhSession ; //会话句柄
HINTERNET FhConnect ; //http连接句柄
HINTERNET FhRequest ; //http请求句柄
HANDLE hFile; //保存内容的文件
string* m_Buffer; //保存内容的缓冲区
public:
CHttpDownLoadFile();
CHttpDownLoadFile(HWND hWnd);
~CHttpDownLoadFile();
public:
LPSTR GetURL() { return m_URL; }
void SetURL( LPSTR Value ) { strcpy(m_URL,Value); }
LPSTR GetOutputFileName() { return m_OutputFileName; }
void SetOutputFileName( LPSTR fn ) { strcpy(m_OutputFileName, fn); m_SaveMode = Mode_SaveToBuffer; }
BOOL SetOverWrite() { return m_OverWrite; }
void SetOverWrite(BOOL Value) { m_OverWrite = Value; }
void SetWndHandle(HWND hWnd) { m_hWnd = hWnd; }
HWND GetWndHandle() { return m_hWnd; }
void SetDownLoadMode(int mode) { m_DownLoadMode= mode; }
int GetDownLoadMode() { return m_DownLoadMode; }
void SetBuffer(string* buffer){ m_Buffer = buffer; m_SaveMode = Mode_SaveToBuffer; }
string* GetBuffer() { return m_Buffer; }
void SetSaveMode(int mode) { m_SaveMode = mode; }
int GetSaveMode() { return m_SaveMode; }
void SetMsgCode(DWORD code) { m_MsgCode = code; }
DWORD GetSpeed() { return m_Speed; }
DWORD GetFileSize() { return m_FileSize; }
DWORD GetBeenDownLoadedSize() { return m_ReadCount; }
public:
int StartDownLoad(); // 开始下载,并把下载的内容保存到文件里。
int StartDownLoad(string* buffer); // 开始下载,把内容保存到缓冲区里。
int StartDownLoad(LPSTR fn); // 开始下载,把内容保存到文件里。
public:
int _RealDownLoad(); // 实际下载
int _SaveToFile(); // 保存到文件
int _SaveToBuffer(); // 保存到缓冲
void ParseURL(char *Url,char *pHostName,char *pFileName,DWORD* nPort);
public:
static int StartDownLoad(LPSTR url,
LPSTR outputFileName,
string* buffer,
int downmode = Mode_BlockNoMsg,
int savemode = Mode_SaveToFile,
HWND hWnd = NULL
);
static ULONG WINAPI DownLoadProc(LPVOID pDownLoadFile);
private:
void DisposeError();
};
#endif
//HttpDownLoadFile.cpp
#include “HttpDownLoadFile.h”
CHttpDownLoadFile::CHttpDownLoadFile()
{
m_hWnd = NULL;
m_DownLoadMode = Mode_BlockNoMsg;
m_SaveMode = Mode_SaveToFile;
m_OverWrite = TRUE;
m_bDownLoadWebOK = FALSE;
m_ErrorCode = 0;
m_Speed = 0;
m_ReadCount = 0;
m_FileSize = 0;
m_MsgCode = DL_MSG;
m_WaitTime = 10000;
hFile = NULL;
m_Buffer = NULL;
memset(m_URL,0x00,sizeof(m_URL));
memset(m_URL,0x00,sizeof(m_OutputFileName));
}
CHttpDownLoadFile::CHttpDownLoadFile(HWND hWnd)
{
m_hWnd = hWnd;
m_DownLoadMode = Mode_BlockNoMsg;
m_SaveMode = Mode_SaveToFile;
m_OverWrite = TRUE;
m_bDownLoadWebOK = FALSE;
m_ErrorCode = 0;
m_Speed = 0;
m_ReadCount = 0;
m_FileSize = 0;
m_MsgCode = DL_MSG;
hFile = NULL;
m_Buffer = NULL;
memset(m_URL,0x00,sizeof(m_URL));
memset(m_URL,0x00,sizeof(m_OutputFileName));
}
int CHttpDownLoadFile::StartDownLoad(string* buffer)
{
this->SetBuffer(buffer);
return this->StartDownLoad();
}
int CHttpDownLoadFile::StartDownLoad(LPSTR fn)
{
this->SetOutputFileName(fn);
return this->StartDownLoad();
}
int CHttpDownLoadFile::StartDownLoad()
{
if( m_DownLoadMode == Mode_BlockNoMsg || m_DownLoadMode == Mode_BlockAndMsg )
{
return this->_RealDownLoad();
}else
{
ULONG threadID;
HANDLE hThread = CreateThread(NULL,0,DownLoadProc,this,0,&threadID);
CloseHandle(hThread);
return TRUE;
}
}
ULONG WINAPI CHttpDownLoadFile::DownLoadProc(LPVOID pDownLoadFile)
{
CHttpDownLoadFile* pDownLoad = (CHttpDownLoadFile*)(pDownLoadFile);
return pDownLoad->_RealDownLoad();
}
int CHttpDownLoadFile::_RealDownLoad()
{
// 发送开始传输消息
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
::SendMessage(m_hWnd,m_MsgCode,DL_START,0);
}
// 打开链接,开始下载
FhSession = ::InternetOpen(“HttpDownLoadFile”, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);// 同步方式
if(FhSession == NULL || FhSession == INVALID_HANDLE_VALUE)
{
//出错处理
this->DisposeError();
return 0;
}
TCHAR server[_MAX_PATH];
TCHAR fileName[_MAX_PATH];
DWORD nPort = INTERNET_DEFAULT_HTTP_PORT;
memset(server,0x00,_MAX_PATH);
memset(fileName,0x00,_MAX_PATH);
const char *FAcceptTypes[] = {“*/*”,NULL};
this->ParseURL(m_URL,server,fileName,&nPort);
// 得到下载后的文件名
if( strcmp(m_OutputFileName,””) == 0)
{
strcpy(m_OutputFileName,fileName);
}
FhConnect = ::InternetConnect(FhSession, server, nPort,
NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if(FhConnect==NULL || FhConnect == INVALID_HANDLE_VALUE)
{
//出错处理
this->DisposeError();
return 0;
}
FhRequest = ::HttpOpenRequest(FhConnect, “GET”, fileName, “HTTP/1.0”, NULL, FAcceptTypes,
INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 0);
if(FhRequest==NULL || FhRequest == INVALID_HANDLE_VALUE)
{
//出错处理
this->DisposeError();
return 0;
}
const char http_accept_header[] = “Accept: */*/r/n”;
if (!::HttpAddRequestHeaders(FhRequest, http_accept_header, strlen(http_accept_header),
HTTP_ADDREQ_FLAG_REPLACE))
{
//出错处理
this->DisposeError();
return 0;
}
const char data_type_header[] = “Content-Type: application/x-www-form-urlencoded/r/n”;
if ( HttpAddRequestHeaders(FhRequest,data_type_header,strlen(data_type_header),
HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE) == FALSE )
{
this->DisposeError();
return 0;
}
if(::HttpSendRequest(FhRequest, NULL, 0, NULL, 0)==FALSE)
{
//出错处理
this->DisposeError();
return 0;
}
// 发送文件大小消息
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
char TmpBuf[64] = “/0″;
DWORD bufLength = 64;
memset(TmpBuf,0x00,64);
if(::HttpQueryInfo(FhRequest, HTTP_QUERY_CONTENT_LENGTH, TmpBuf,&bufLength, NULL)==FALSE)
{
::SendMessage(m_hWnd, DL_MSG, DL_ERROR, 0);
}
else
{
this->m_FileSize = atoi(TmpBuf);
::SendMessage(m_hWnd, DL_MSG, DL_FILESIZE, m_FileSize);
}
}
if( this->m_SaveMode == Mode_SaveToFile )
{
this->_SaveToFile();
}else
{
this->_SaveToBuffer();
}
if(FhRequest) {
::InternetCloseHandle(FhRequest);
FhRequest = NULL;
}
if(FhConnect) {
::InternetCloseHandle(FhConnect);
FhConnect = NULL;
}
if(FhSession) {
::InternetCloseHandle(FhSession);
FhSession = NULL;
}
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
::SendMessage(m_hWnd,m_MsgCode,DL_TERMINATE,0);
}
return TRUE;
}
// 保存到文件
int CHttpDownLoadFile::_SaveToFile()
{
// 创建文件
hFile = CreateFile(m_OutputFileName,GENERIC_WRITE,FILE_SHARE_READ,NULL, /
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if( hFile == INVALID_HANDLE_VALUE )
{
return FALSE;
}
const int bufLength = 4096;
char buf[bufLength];
DWORD realWrite = 0;
DWORD dwRead = 0;
DWORD bRet = 0;
memset(buf,0,bufLength);
DWORD tick = ::timeGetTime();
while(1)
{
bRet = ::InternetReadFile(FhRequest, (LPVOID)(buf), bufLength, &dwRead);
if(dwRead<=0) break; //已读到文件尾
if(bRet==FALSE)
{
this->DisposeError();
return 0;
}
m_ReadCount = m_ReadCount+dwRead;
if(::WriteFile(hFile,(void *)buf,dwRead,&realWrite,(LPOVERLAPPED)NULL)==0)
{//写操作出错处理
this->DisposeError();
return 0;
}
if(::timeGetTime()-tick > 0)
{
m_Speed=(m_ReadCount/(::timeGetTime()-tick))*1000;
}
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
::SendMessage((HWND)m_hWnd, DL_MSG, DL_READDATA, (LPARAM)this);
}
}
if(hFile)
{
::CloseHandle(hFile);
hFile = NULL;
}
return 1;
}
// 保存到缓冲
int CHttpDownLoadFile::_SaveToBuffer()
{
const int bufLength = 4096;
char buf[bufLength];
DWORD dwRead = 0;
DWORD bRet = 0;
memset(buf,0,bufLength);
DWORD tick = ::timeGetTime();
if( m_Buffer == NULL )
{
m_Buffer = new string;
}
while(1)
{
bRet = ::InternetReadFile(FhRequest, (LPVOID)(buf), bufLength, &dwRead);
if(dwRead<=0) break; //已读到文件尾
m_ReadCount = m_ReadCount+dwRead;
*m_Buffer += buf;
if(::timeGetTime()-tick > 0)
{
m_Speed=(m_ReadCount/(::timeGetTime()-tick))*1000;
}
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
::SendMessage((HWND)m_hWnd, DL_MSG, DL_READDATA, (LPARAM)this);
}
}
return 1;
}
int CHttpDownLoadFile::StartDownLoad(LPSTR url,
LPSTR outputFileName,
string* buffer,
int downmode,
int savemode,
HWND hWnd)
{
CHttpDownLoadFile* pDownLoad = new CHttpDownLoadFile;
pDownLoad->SetURL(url);
pDownLoad->SetOutputFileName(outputFileName);
pDownLoad->SetBuffer(buffer);
pDownLoad->SetDownLoadMode(downmode);
pDownLoad->SetSaveMode(savemode);
pDownLoad->SetWndHandle(hWnd);
return pDownLoad->StartDownLoad();
}
void CHttpDownLoadFile::ParseURL(char *Url,char *pHostName,char *pFileName,DWORD* nPort)
{
char *all=Url;
char *p=strstr(all,”http://”);
char Buffer[_MAX_PATH];
memset(Buffer,0x00,_MAX_PATH);
if(p)
{
int pos=p-all;
all=all+pos+strlen(“http://”);
p=strstr(all,”/”);
if(p)
{
pos=p-all;
memcpy(Buffer,all,pos);
strcpy(pFileName,all+pos);
p = strstr(Buffer,”:”);
if(p)
{
pos = p – Buffer;
memcpy(pHostName,Buffer,pos);
*nPort = atoi(p+1);
}else
{
strcpy(pHostName,Buffer);
}
}
}
}
void CHttpDownLoadFile::DisposeError()
{
if(FhRequest) {
::InternetCloseHandle(FhRequest);
FhRequest = NULL;
}
if(FhConnect) {
::InternetCloseHandle(FhConnect);
FhConnect = NULL;
}
if(FhSession) {
::InternetCloseHandle(FhSession);
FhSession = NULL;
}
if(hFile) {
::CloseHandle(hFile);
hFile = NULL;
}
if( m_DownLoadMode == Mode_UnBlockAndMsg || m_DownLoadMode == Mode_BlockAndMsg)
{
::SendMessage(m_hWnd,m_MsgCode,DL_ERROR,0);
}
}