[转帖]HTTP实现断点再续

简介: 从HTTP服务器上下载一个文件有很多方法,“热心”的微软提供了 WinInet 类,用起来也很方便。当然,我们也可以自己实现这些功能,通过格式化请求头很容易就能实现断点续传和检查更新等等功能 。
从HTTP服务器上下载一个文件有很多方法,“热心”的微软提供了 WinInet 类,用起来也很方便。当然,我们也可以自己实现这些功能,通过格式化请求头很容易就能实现断点续传和检查更新等等功能 。 
1. 连接主机 
2. 格式化请求头 
3. 设置接收,发送超时 
  
要想从服务器下载文件,首先要向服务器发送一个请求。HTTP 请求头由若干行字符串组成。下面结合实例说说 HTTP 请求头的格式。假设要下载  http://www.sina.com.cn/index.html  这个网页 ,那么请求头的写法如下:

第1行:方法,请求的内容,HTTP协议的版本
下载一般可以用GET方法,请求的内容是“/index.html”,HTTP协议的版本是指浏览器支持的版本,对于下载软件来说无所谓,所以用1.1版 “HTTP/1.1”;
“GET /index.html HTTP/1.1”

第2行:主机名,格式为“Host:主机”
在这个例子中是:“Host:www.sina.com.cn”

第3行:接受的数据类型,下载软件当然要接收所有的数据类型,所以:
“Accept:*/*”

第4行:指定浏览器的类型
有些服务器会根据客户服务器种类的不同会增加或减少一些内容,在这个例子中可以这样写:
“User-Agent:Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)”
第5行:连接设置
设定为一直保持连接:“Connection:Keep-Alive”

第6行:若要实现断点续传则要指定从什么位置起接收数据,格式如下:
“Range: bytes=起始位置 - 终止位置”
比如要读前500个字节可以这样写:“Range: bytes=0 - 499”;从第 1000 个字节起开始下载:
“Range: bytes=999 -”
最后,别忘了加上一行空行,表示请求头结束。整个请求头如下:
GET /index.html HTTP/1.1
Host:www.sina.com.cn
Accept:*/*
User-Agent:Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)
Connection:Keep-Alive
下面用例子看看如何进行断点的下载吧
None.gif //  DownloadFile.h: interface for the CDownloadFile class.
None.gif
//
ExpandedBlockStart.gif
//////////////////////////////////////////////////////////////////////
None.gif
None.gif #if !defined(AFX_DOWNLOADFILE_H__E9A59779_BEF9_4A78_8D0E_ED8C9498E07C__INCLUDED_)
None.gif #define AFX_DOWNLOADFILE_H__E9A59779_BEF9_4A78_8D0E_ED8C9498E07C__INCLUDED_
None.gif
None.gif #if _MSC_VER > 1000
None.gif#pragma once
None.gif #endif  //  _MSC_VER > 1000
None.gif
None.gif #define NOTIFY_MSG_WPARAM_GENDOWNFILEID 0x01
None.gif
None.gif #define NOTIFY_MSG_LOW_WPARAM_FULLSIZE 0x10
None.gif #define NOTIFY_MSG_LOW_WPARAM_CURRENTSIZE 0x20
None.gif #define NOTIFY_MSG_LOW_WPARAM_DOWNSIZE 0x30
None.gif #define NOTIFY_MSG_LOW_WPARAM_DOWNSPEED 0x40
None.gif
None.gif class CDownloadFile 
ExpandedBlockStart.gif {
InBlock.gifpublic:
InBlock.gifBOOL OpenRedirectHttpURL(CString &strOldLocation,CInternetSession &cSession);
InBlock.gifBOOL DownLoadFile(LPCTSTR lpFileURL,LPCTSTR lpSaveFile);
InBlock.gifCDownloadFile();
InBlock.gifvirtual ~CDownloadFile();
ExpandedSubBlockStart.gifLPCTSTR GetSavedFileName() return m_strSaveToFile;}
ExpandedSubBlockStart.gifLPCTSTR GetDownURL() return m_strFileURL;}
InBlock.gif
InBlock.gifpublic:
InBlock.gifWORD GenFileID();
InBlock.gifvoid RegisterNotifyWindow(DWORD dwThreadID,HWND hWnd,DWORD dwMsg);
InBlock.gifBOOL GetUNCFile();
InBlock.gifbool m_bForceReload;
InBlock.gifDWORD m_TimeOut;
InBlock.gifWORD m_wFileID;
InBlock.gif
InBlock.gifprotected:
InBlock.gifDWORD m_dwThreadID;
InBlock.gifvoid PostNotifyMessage(WPARAM wParam, LPARAM lParam);
InBlock.gifDWORD m_dwMsgID;
InBlock.gifHWND m_hNotify;
InBlock.gifBOOL GetFtpFile(CInternetSession &cSession);
InBlock.gifBOOL GetHttpFile(CInternetSession &cSession);
InBlock.gifCString m_strTmpFileName;
InBlock.gifCString m_strFileURL;
InBlock.gifCString m_strSaveToFile;
InBlock.gifCString m_rawHeaders;
InBlock.giffloat m_transferRate;
InBlock.gifDWORD m_infoStatusCode;
ExpandedBlockEnd.gif}
;
None.gif
None.gif #endif  //  !defined(AFX_DOWNLOADFILE_H__E9A59779_BEF9_4A78_8D0E_ED8C9498E07C__INCLUDED_)
None.gif

None.gif //  DownloadFile.cpp: implementation of the CDownloadFile class.
None.gif
//
ExpandedBlockStart.gif
//////////////////////////////////////////////////////////////////////
None.gif
None.gif#include "stdafx.h"
None.gif#include "DownloadFile.h"
None.gif
None.gif#pragma comment( lib,"Wininet.lib" )
None.gif
None.gif#include "shlwapi.h"
None.gif#pragma comment( lib,"shlwapi.lib" )
None.gif
None.gif#ifdef _DEBUG
None.gif #undef THIS_FILE
None.gif static  char THIS_FILE[]=__FILE__;
None.gif #define new DEBUG_NEW
None.gif #endif
None.gif
None.gif #define BUFFER_SIZE 4095
None.gif
None.gif const TCHAR szHeaders[] = _T("Accept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n");
None.gif
ExpandedBlockStart.gif //////////////////////////////////////////////////////////////////////
None.gif //  Construction/Destruction
ExpandedBlockStart.gif
//////////////////////////////////////////////////////////////////////
None.gif
None.gifCDownloadFile::CDownloadFile()
ExpandedBlockStart.gif {
InBlock.gifm_TimeOut = 0;
InBlock.gifm_bForceReload = true;
InBlock.gifm_dwThreadID = 0;
InBlock.gifm_hNotify = NULL;
InBlock.gifm_dwMsgID = 0;
InBlock.gifm_wFileID = 0;
ExpandedBlockEnd.gif}

None.gif
None.gifCDownloadFile::~CDownloadFile()
ExpandedBlockStart.gif {
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifBOOL CDownloadFile::DownLoadFile(LPCTSTR lpFileURL, LPCTSTR lpSaveFile)
ExpandedBlockStart.gif {
InBlock.gifBOOL bRet = FALSE;
InBlock.gifif ( !::PathIsURL(lpFileURL) )
ExpandedSubBlockStart.gif{
InBlock.gifreturn bRet;
ExpandedSubBlockEnd.gif}

InBlock.gifm_strSaveToFile = lpSaveFile;
InBlock.gifm_strFileURL = lpFileURL;
InBlock.gifm_strTmpFileName = lpSaveFile;
InBlock.gifm_strTmpFileName += _T(".df!");
InBlock.gifCString strServer,strObject;INTERNET_PORT nPort;
InBlock.gifCString strAgentCaption = _T("Update Download ") ;
InBlock.gifstrAgentCaption += ::PathFindFileName(lpSaveFile);
InBlock.gifDWORD dwFlags = 0;
InBlock.gifInternetGetConnectedState(&dwFlags, 0);
InBlock.gifCInternetSession session (strAgentCaption, 1,
InBlock.gif
InBlock.gif(dwFlags & INTERNET_CONNECTION_PROXY) == INTERNET_CONNECTION_PROXY ? INTERNET_OPEN_TYPE_PRECONFIG : INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY,
InBlock.gifNULL, NULL, 0);
InBlock.gifAfxParseURL(m_strFileURL,dwFlags,strServer,strObject,nPort);
InBlock.gif
InBlock.gifif (m_TimeOut != 0)
InBlock.gifsession.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, m_TimeOut);
InBlock.gifif( !m_wFileID )
InBlock.gifm_wFileID = GenFileID();
InBlock.gifPostNotifyMessage(NOTIFY_MSG_WPARAM_GENDOWNFILEID,m_wFileID);
InBlock.gif
InBlock.giftry
ExpandedSubBlockStart.gif{
InBlock.gifif ( dwFlags== AFX_INET_SERVICE_HTTP )
ExpandedSubBlockStart.gif{
InBlock.gifbRet = GetHttpFile(session);
ExpandedSubBlockEnd.gif}

InBlock.gifelse if( dwFlags== AFX_INET_SERVICE_FTP )
ExpandedSubBlockStart.gif{
InBlock.gifbRet = GetFtpFile(session);
ExpandedSubBlockEnd.gif}

InBlock.gifelse if( dwFlags== AFX_INET_SERVICE_FILE )
ExpandedSubBlockStart.gif{
InBlock.gifif( UrlIsFileUrl(m_strFileURL) )
InBlock.gifbRet = GetUNCFile();
ExpandedSubBlockEnd.gif}

InBlock.gifelse
ExpandedSubBlockStart.gif{
InBlock.gif;
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}

InBlock.gifcatch (CException* pEx)
ExpandedSubBlockStart.gif{
ExpandedSubBlockStart.gifTCHAR szErrorMsg[MAX_PATH] = {0};
InBlock.gifpEx->GetErrorMessage(szErrorMsg, MAX_PATH);
InBlock.gifTRACE( _T("Exception: %s\n") , szErrorMsg);
InBlock.gifpEx->Delete();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifsession.Close();
InBlock.gifm_wFileID = 0;
InBlock.gifif (bRet)
ExpandedSubBlockStart.gif{
InBlock.gifif (!::MoveFileEx(m_strTmpFileName,m_strSaveToFile,MOVEFILE_REPLACE_EXISTING) )
ExpandedSubBlockStart.gif{
InBlock.gifSleep(1000);
InBlock.gif::MoveFileEx(m_strTmpFileName,m_strSaveToFile,MOVEFILE_REPLACE_EXISTING);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifreturn bRet;
ExpandedBlockEnd.gif}

None.gif
None.gifBOOL CDownloadFile::GetHttpFile(CInternetSession &cSession)
ExpandedBlockStart.gif {
InBlock.gifBOOL bRet = FALSE;
InBlock.gifCFile m_TmpFile;
InBlock.gifCFileException fileException;
InBlock.gif
InBlock.gifif ( !m_TmpFile.Open (m_strTmpFileName, 
InBlock.gifCFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite
InBlock.gif| CFile::shareDenyWrite | CFile::typeBinary,
InBlock.gif&fileException ) )
ExpandedSubBlockStart.gif{
InBlock.gifTRACE( _T("Open File failed: %d\n"), fileException.m_cause );
InBlock.gifreturn bRet;
ExpandedSubBlockEnd.gif}

InBlock.gifCString strRangeQuest;
InBlock.gif
InBlock.gifif (m_TmpFile.GetLength()>0)
ExpandedSubBlockStart.gif{
InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_CURRENTSIZE,m_wFileID),m_TmpFile.GetLength());
InBlock.gifm_TmpFile.SeekToEnd();
InBlock.gifstrRangeQuest.Format( _T("%sRange: bytes=%d-\r\n"), szHeaders,m_TmpFile.GetLength());
ExpandedSubBlockEnd.gif}

InBlock.gifelse
InBlock.gifstrRangeQuest = szHeaders;
InBlock.gif
InBlock.gifDWORD dwCount = 0;
InBlock.gifCHttpFile* pFile = NULL;
InBlock.gifCString strTmpURL = m_strFileURL;
InBlock.giftry
ExpandedSubBlockStart.gif{
InBlock.gifDWORD dwFlags = INTERNET_FLAG_TRANSFER_BINARY 
InBlock.gif|INTERNET_FLAG_DONT_CACHE
InBlock.gif|INTERNET_FLAG_PRAGMA_NOCACHE
InBlock.gif;
ExpandedSubBlockStart.gifif (m_bForceReload) {
InBlock.gifdwFlags |= INTERNET_FLAG_RELOAD;
ExpandedSubBlockEnd.gif}

InBlock.gif//Here Find URLFile Redirect.
InBlock.gif
// OpenRedirectHttpURL(strTmpURL,cSession);
InBlock.gif
pFile = (CHttpFile*) cSession.OpenURL(strTmpURL, 1, dwFlags,strRangeQuest, -1);
ExpandedSubBlockEnd.gif}

InBlock.gifcatch (CInternetException* e)
ExpandedSubBlockStart.gif{
ExpandedSubBlockStart.gifTCHAR szCause[MAX_PATH] = {0};
InBlock.gife->GetErrorMessage(szCause, MAX_PATH);
InBlock.gife->Delete();
InBlock.gifdelete pFile;
InBlock.gifpFile = NULL;
InBlock.gifreturn bRet;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifCOleDateTime startTime = COleDateTime::GetCurrentTime();
InBlock.gifDWORD dwHttpFileSize = 0;
InBlock.gifif (pFile)
ExpandedSubBlockStart.gif{
ExpandedSubBlockStart.gifBYTE buffer[BUFFER_SIZE+1] = {0};
ExpandedSubBlockStart.giftry {
InBlock.gifUINT nRead = 0;
InBlock.gifpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,dwHttpFileSize);
InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_FULLSIZE,m_wFileID),dwHttpFileSize);
InBlock.gifTRACE( _T("Totoal Length is %d\n"), dwHttpFileSize );
InBlock.gifdwCount = 0;
InBlock.gifdo
ExpandedSubBlockStart.gif{
InBlock.gifnRead = pFile->Read(buffer, BUFFER_SIZE);
InBlock.gifif (nRead > 0)
ExpandedSubBlockStart.gif{
InBlock.gifbuffer[nRead] = 0;
InBlock.gifm_TmpFile.Write(buffer,nRead);
InBlock.gif
InBlock.gifCOleDateTimeSpan elapsed = COleDateTime::GetCurrentTime() - startTime;
InBlock.gifdouble dSecs = elapsed.GetTotalSeconds();
InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_DOWNSIZE,m_wFileID),dwCount);
InBlock.gifif (dSecs > 0.0)
ExpandedSubBlockStart.gif{
InBlock.gifdwCount += nRead;
InBlock.gifm_transferRate = (float) ( dwCount / 1024.0 / dSecs );
InBlock.gifTRACE("Read %d bytes (%0.1f Kb/s)\n", dwCount, m_transferRate );
ExpandedSubBlockEnd.gif}

InBlock.gifelse
ExpandedSubBlockStart.gif{
InBlock.gifTRACE("Read %d bytes\n", dwCount);
InBlock.gifm_transferRate = (float) ( dwCount / 1024.0 );
ExpandedSubBlockEnd.gif}

InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_DOWNSPEED,m_wFileID),(LPARAM)m_transferRate);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifwhile (nRead > 0);
InBlock.gifbRet = TRUE;
ExpandedSubBlockEnd.gif}

InBlock.gifcatch (CFileException *e)
ExpandedSubBlockStart.gif{
ExpandedSubBlockStart.gifTCHAR szCause[MAX_PATH] = {0};
InBlock.gife->GetErrorMessage(szCause, MAX_PATH);
InBlock.gifTRACE("ErrorMsg : %s\n", szCause);
InBlock.gife->Delete();
InBlock.gifdelete pFile;
InBlock.gifm_TmpFile.Close();
InBlock.gifreturn FALSE;
ExpandedSubBlockEnd.gif}

InBlock.gifpFile->QueryInfoStatusCode(m_infoStatusCode); 
InBlock.gifpFile->QueryInfo(HTTP_QUERY_RAW_HEADERS ,m_rawHeaders);
InBlock.gifpFile->Close();
InBlock.gifm_TmpFile.Close();
InBlock.gifdelete pFile;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifreturn bRet;
ExpandedBlockEnd.gif}

None.gif
None.gifBOOL CDownloadFile::OpenRedirectHttpURL(CString &strOldLocation,CInternetSession &cSession)
ExpandedBlockStart.gif {
InBlock.gifBOOL bRet = FALSE;
InBlock.gifCHttpFile *pFile = NULL;
InBlock.gifCHttpConnection* pServer = NULL;
InBlock.gifCString strServerName,strObject;
InBlock.gifINTERNET_PORT nPort = 0;
InBlock.gifDWORD dwServiceType = 0;
InBlock.gif
InBlock.gifif (!AfxParseURL(strOldLocation, dwServiceType, strServerName, strObject, nPort) ||
InBlock.gifdwServiceType != INTERNET_SERVICE_HTTP)
ExpandedSubBlockStart.gif{
InBlock.gifTRACE( _T("Not A Http Quest!\n") );
InBlock.gifreturn bRet;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifpServer = cSession.GetHttpConnection(strServerName, nPort);
InBlock.gif
InBlock.gifpFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
InBlock.gifstrObject, NULL, 1, NULL, NULL, 
InBlock.gifINTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT);
InBlock.gifpFile->AddRequestHeaders(szHeaders);
InBlock.gifpFile->SendRequest();
InBlock.gif
InBlock.gifDWORD dwRet;
InBlock.gifpFile->QueryInfoStatusCode(dwRet);
InBlock.gif
InBlock.gif// if access was denied, prompt the user for the password
InBlock.gif

InBlock.gifif (dwRet == HTTP_STATUS_DENIED)
ExpandedSubBlockStart.gif{
InBlock.gifDWORD dwPrompt;
InBlock.gifdwPrompt = pFile->ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD,
InBlock.gifFLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL);
InBlock.gif
InBlock.gif// if the user cancelled the dialog, bail out
InBlock.gif

InBlock.gifif (dwPrompt != ERROR_INTERNET_FORCE_RETRY)
ExpandedSubBlockStart.gif{
InBlock.gifTRACE( _T("Access denied: Invalid password\n") );
InBlock.gif// close up the redirected site
InBlock.gif

InBlock.gifpFile->Close();
InBlock.gifdelete pFile;
InBlock.gifpServer->Close();
InBlock.gifdelete pServer;
InBlock.gif
InBlock.gifreturn bRet;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifpFile->SendRequest();
InBlock.gifpFile->QueryInfoStatusCode(dwRet);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif// were we redirected?
InBlock.gif
// these response status codes come from WININET.H
InBlock.gif

InBlock.gifif (dwRet == HTTP_STATUS_MOVED ||
InBlock.gifdwRet == HTTP_STATUS_REDIRECT ||
InBlock.gifdwRet == HTTP_STATUS_REDIRECT_METHOD)
ExpandedSubBlockStart.gif{
InBlock.gifCString strNewLocation;
InBlock.gifpFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
InBlock.gifint nPlace = strNewLocation.Find(_T("Location: "));
InBlock.gifif (nPlace == -1)
ExpandedSubBlockStart.gif{
InBlock.gifTRACE( _T("Error: Site redirects with no new location\n") );
InBlock.gif// close up the redirected site
InBlock.gif

InBlock.gifpFile->Close();
InBlock.gifdelete pFile;
InBlock.gifpServer->Close();
InBlock.gifdelete pServer;
InBlock.gifreturn bRet;
ExpandedSubBlockEnd.gif}
 
InBlock.gifstrNewLocation = strNewLocation.Mid(nPlace + 10);
InBlock.gifnPlace = strNewLocation.Find('\n');
InBlock.gifif (nPlace > 0)
InBlock.gifstrNewLocation = strNewLocation.Left(nPlace);
InBlock.gifstrOldLocation = strNewLocation;
ExpandedSubBlockEnd.gif}

InBlock.gifif ( dwRet == HTTP_STATUS_OK )
ExpandedSubBlockStart.gif{
InBlock.gifbRet = TRUE;
ExpandedSubBlockEnd.gif}

InBlock.gif// close up the redirected site
InBlock.gif
pFile->Close();
InBlock.gifdelete pFile;
InBlock.gifpServer->Close();
InBlock.gifdelete pServer;
InBlock.gif
InBlock.gifreturn bRet;
ExpandedBlockEnd.gif}

None.gif
None.gifBOOL CDownloadFile::GetFtpFile(CInternetSession &cSession)
ExpandedBlockStart.gif {
InBlock.gifBOOL bRet = FALSE;
InBlock.gifCFile m_TmpFile;
InBlock.gifCFileException fileException;
InBlock.gif
InBlock.gifif ( !m_TmpFile.Open (m_strTmpFileName, 
InBlock.gifCFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite
InBlock.gif| CFile::shareDenyWrite | CFile::typeBinary,
InBlock.gif&fileException ) )
ExpandedSubBlockStart.gif{
InBlock.gifTRACE( _T("Open File failed: %d\n"), fileException.m_cause );
InBlock.gifreturn bRet;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifDWORD dwCount = 0;
InBlock.gifCFtpConnection *pFtpConn = NULL;
InBlock.gifCInternetFile *pFile = NULL;
InBlock.giftry
ExpandedSubBlockStart.gif{
InBlock.gifCString strServerName,strObject,strUserName,strPassword;
InBlock.gifINTERNET_PORT nPort = 0;
InBlock.gifDWORD dwServiceType = 0;
InBlock.gifCString strRestPointCommand;
InBlock.gif
InBlock.gifif (!AfxParseURLEx(m_strFileURL, dwServiceType, strServerName, strObject, nPort, 
InBlock.gifstrUserName, strPassword) ||
InBlock.gifdwServiceType != INTERNET_SERVICE_FTP)
ExpandedSubBlockStart.gif{
InBlock.gifTRACE( _T("Not A Ftp Quest!\n") );
ExpandedSubBlockEnd.gif}

InBlock.gif// CFtpConnection ERROR_INTERNET_NO_DIRECT_ACCESS CInternetSession
InBlock.gif
if (strUserName.IsEmpty())
InBlock.gifpFtpConn = cSession.GetFtpConnection(strServerName,NULL,NULL,nPort,m_bForceReload);
InBlock.gifelse
InBlock.gifpFtpConn = cSession.GetFtpConnection(strServerName,strUserName,strPassword,nPort,m_bForceReload);
InBlock.gifif (m_TmpFile.GetLength())
ExpandedSubBlockStart.gif{
InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_CURRENTSIZE,m_wFileID),m_TmpFile.GetLength());
InBlock.gifm_TmpFile.SeekToEnd();
InBlock.gifstrRestPointCommand.Format( _T("REST %d"), m_TmpFile.GetLength());
InBlock.gif//strRestPointCommand.Format( _T("ls") );
InBlock.gif
if ( !FtpCommand((*pFtpConn), FALSE, FTP_TRANSFER_TYPE_ASCII, 
InBlock.gifstrRestPointCommand, 0, 0) )
ExpandedSubBlockStart.gif{
InBlock.gifTRACE( _T("FtpCommand failed, error: %d\n"), GetLastError());
InBlock.gifm_TmpFile.SeekToBegin();
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}

InBlock.gifif (pFtpConn)
ExpandedSubBlockStart.gif{
InBlock.gifpFile = pFtpConn->OpenFile(strObject);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifcatch (CInternetException* e)
ExpandedSubBlockStart.gif{
ExpandedSubBlockStart.gifTCHAR szCause[MAX_PATH] = {0};
InBlock.gife->GetErrorMessage(szCause, MAX_PATH);
InBlock.gife->Delete();
InBlock.gifdelete pFile;
InBlock.gifdelete pFtpConn;
InBlock.gifreturn bRet;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifCOleDateTime startTime = COleDateTime::GetCurrentTime();
InBlock.gifDWORD dwFtpFileSize = 0;
InBlock.gifif (pFile)
ExpandedSubBlockStart.gif{
ExpandedSubBlockStart.gifBYTE buffer[BUFFER_SIZE+1] = {0};
ExpandedSubBlockStart.giftry {
InBlock.gifUINT nRead = 0;
InBlock.gifdwFtpFileSize = FtpGetFileSize( (*pFile),0);
InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_FULLSIZE,m_wFileID),dwFtpFileSize);
InBlock.gifTRACE( _T("Totoal Length is %d\n"), dwFtpFileSize );
InBlock.gifdwCount = 0;
InBlock.gifdo
ExpandedSubBlockStart.gif{
InBlock.gifnRead = pFile->Read(buffer, BUFFER_SIZE);
InBlock.gifif (nRead > 0)
ExpandedSubBlockStart.gif{
InBlock.gifbuffer[nRead] = 0;
InBlock.gifm_TmpFile.Write(buffer,nRead);
InBlock.gif
InBlock.gifCOleDateTimeSpan elapsed = COleDateTime::GetCurrentTime() - startTime;
InBlock.gifdouble dSecs = elapsed.GetTotalSeconds();
InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_DOWNSIZE,m_wFileID),dwCount);
InBlock.gifif (dSecs > 0.0)
ExpandedSubBlockStart.gif{
InBlock.gifdwCount += nRead;
InBlock.gifm_transferRate = (float)(dwCount / 1024.0 / dSecs);
InBlock.gifTRACE("Read %d bytes (%0.1f Kb/s)\n", dwCount, m_transferRate ); 
ExpandedSubBlockEnd.gif}

InBlock.gifelse
ExpandedSubBlockStart.gif{
InBlock.gifTRACE("Read %d bytes\n", dwCount);
InBlock.gifm_transferRate = (float)(dwCount / 1024.0);
ExpandedSubBlockEnd.gif}

InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_DOWNSPEED,m_wFileID),(LPARAM)m_transferRate);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifwhile (nRead > 0);
InBlock.gifbRet = TRUE;
ExpandedSubBlockEnd.gif}

InBlock.gifcatch (CFileException *e)
ExpandedSubBlockStart.gif{
ExpandedSubBlockStart.gifTCHAR szCause[MAX_PATH] = {0};
InBlock.gife->GetErrorMessage(szCause, MAX_PATH);
InBlock.gifTRACE("ErrorMsg : %s\n", szCause);
InBlock.gife->Delete();
InBlock.gifdelete pFile;
InBlock.gifpFtpConn->Close();
InBlock.gifdelete pFtpConn;
InBlock.gifm_TmpFile.Close();
InBlock.gifreturn FALSE;
ExpandedSubBlockEnd.gif}

InBlock.gifpFile->Close();
InBlock.gifdelete pFile;
InBlock.gifm_TmpFile.Close();
InBlock.gifpFtpConn->Close();
InBlock.gifdelete pFtpConn;
ExpandedSubBlockEnd.gif}

InBlock.gifreturn bRet;
ExpandedBlockEnd.gif}

None.gif
None.gifBOOL CDownloadFile::GetUNCFile()
ExpandedBlockStart.gif {
InBlock.gifBOOL bRet = FALSE;
InBlock.gifCFile m_TmpFile,m_SrcFile;
InBlock.gifCFileException fileException;
InBlock.gifCString strOldLocation = m_strFileURL;
InBlock.gifif ( !m_TmpFile.Open (m_strTmpFileName, 
InBlock.gifCFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite
InBlock.gif| CFile::shareDenyWrite | CFile::typeBinary,
InBlock.gif&fileException ) )
ExpandedSubBlockStart.gif{
InBlock.gifTRACE( _T("Open File failed: %d\n"), fileException.m_cause );
InBlock.gifreturn bRet;
ExpandedSubBlockEnd.gif}

InBlock.gifstrOldLocation.TrimLeft();
InBlock.gifstrOldLocation.TrimRight();
InBlock.gifif( StrCmpNI(strOldLocation, _T("file:/"),6) == 0 )
ExpandedSubBlockStart.gif{
InBlock.gifstrOldLocation = strOldLocation.Mid(8);
InBlock.gifstrOldLocation.Replace( _T('/'), _T('\\'));
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifif ( !m_SrcFile.Open ( strOldLocation, 
InBlock.gifCFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary,
InBlock.gif&fileException ) )
ExpandedSubBlockStart.gif{
InBlock.gifTRACE( _T("Open File failed: %d\n"), fileException.m_cause );
InBlock.gifreturn bRet;
ExpandedSubBlockEnd.gif}

InBlock.gifCOleDateTime startTime = COleDateTime::GetCurrentTime();
InBlock.gifDWORD dwCount = 0;
ExpandedSubBlockStart.giftry {
InBlock.gifif (m_TmpFile.GetLength())
ExpandedSubBlockStart.gif{
InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_CURRENTSIZE,m_wFileID),m_TmpFile.GetLength());
InBlock.gifm_TmpFile.SeekToEnd();
InBlock.gifm_SrcFile.Seek(m_TmpFile.GetLength(), CFile::begin);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifBYTE buffer[BUFFER_SIZE+1] = {0};
InBlock.gifUINT nRead = 0;
InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_FULLSIZE,m_wFileID),m_SrcFile.GetLength());
InBlock.gifTRACE( _T("Totoal Length is %d,left is %d\n"), m_SrcFile.GetLength() ,m_SrcFile.GetLength() - m_TmpFile.GetLength());
InBlock.gifdwCount = 0;
InBlock.gifdo
ExpandedSubBlockStart.gif{
InBlock.gifnRead = m_SrcFile.Read(buffer, BUFFER_SIZE);
InBlock.gifif (nRead > 0)
ExpandedSubBlockStart.gif{
InBlock.gifbuffer[nRead] = 0;
InBlock.gifm_TmpFile.Write(buffer,nRead);
InBlock.gif
InBlock.gifCOleDateTimeSpan elapsed = COleDateTime::GetCurrentTime() - startTime;
InBlock.gifdouble dSecs = elapsed.GetTotalSeconds();
InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_DOWNSIZE,m_wFileID),dwCount);
InBlock.gifif (dSecs > 0.0)
ExpandedSubBlockStart.gif{
InBlock.gifdwCount += nRead;
InBlock.gifm_transferRate = (float)(dwCount / 1024.0 / dSecs);
InBlock.gifTRACE("Read %d bytes (%0.1f Kb/s)\n", dwCount, m_transferRate ); 
ExpandedSubBlockEnd.gif}

InBlock.gifelse
ExpandedSubBlockStart.gif{
InBlock.gifTRACE("Read %d bytes\n", dwCount);
InBlock.gifm_transferRate = (float)(dwCount / 1024.0);
ExpandedSubBlockEnd.gif}

InBlock.gifPostNotifyMessage(MAKEWPARAM(NOTIFY_MSG_LOW_WPARAM_DOWNSPEED,m_wFileID),(LPARAM)m_transferRate);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifwhile (nRead > 0);
InBlock.gifbRet = TRUE;
ExpandedSubBlockEnd.gif}

InBlock.gifcatch (CFileException *e)
ExpandedSubBlockStart.gif{
ExpandedSubBlockStart.gifTCHAR szCause[MAX_PATH] = {0};
InBlock.gife->GetErrorMessage(szCause, MAX_PATH);
InBlock.gifTRACE("ErrorMsg : %s\n", szCause);
InBlock.gife->Delete();
InBlock.gifm_TmpFile.Close();
InBlock.gifreturn FALSE;
ExpandedSubBlockEnd.gif}

InBlock.gifm_TmpFile.Close();
InBlock.gifreturn bRet;
ExpandedBlockEnd.gif}

None.gif
None.gif void CDownloadFile::RegisterNotifyWindow(DWORD dwThreadID,HWND hWnd, DWORD dwMsg)
ExpandedBlockStart.gif {
InBlock.gifm_dwThreadID = dwThreadID;
InBlock.gifm_hNotify = hWnd;
InBlock.gifm_dwMsgID = dwMsg;
ExpandedBlockEnd.gif}

None.gif
None.gif void CDownloadFile::PostNotifyMessage(WPARAM wParam, LPARAM lParam)
ExpandedBlockStart.gif {
InBlock.gifif (m_hNotify)
ExpandedSubBlockStart.gif{
InBlock.gif::PostMessage(m_hNotify, m_dwMsgID, wParam, lParam);
ExpandedSubBlockEnd.gif}

InBlock.gifif ( m_dwThreadID )
ExpandedSubBlockStart.gif{
InBlock.gif::PostThreadMessage(m_dwThreadID,m_dwMsgID, wParam, lParam);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gifWORD CDownloadFile::GenFileID()
ExpandedBlockStart.gif {
InBlock.gifsrand(GetTickCount());
InBlock.gifreturn rand() & 0xFFFF;
ExpandedBlockEnd.gif}

None.gifUINT ThreadDownSingleFile( LPVOID pParam )
ExpandedBlockStart.gif {
InBlock.gifCDownloadFile m_DownFile;
InBlock.gifUINT uRet = 0;
InBlock.gifif (lpDownParam)
ExpandedSubBlockStart.gif{
InBlock.gifm_DownFile.m_wFileID = m_DownFile.GenFileID();
InBlock.gif//这里注册通知窗口和消息
InBlock.gif
//m_DownFile.RegisterNotifyWindow
InBlock.gif
if ( m_DownFile.DownLoadFile(m_Msg.lpFileSrc, m_Msg.lpFileDst) )
ExpandedSubBlockStart.gif{
InBlock.gifuRet = 1;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifreturn uRet;
ExpandedBlockEnd.gif}

None.gif
目录
相关文章
|
1月前
|
前端开发
webpack如何设置devServer启动项目为https协议
webpack如何设置devServer启动项目为https协议
135 0
|
3月前
|
Web App开发 移动开发 JavaScript
Python网络编程(三),HTTP协议
Python网络编程(三),HTTP协议
60 0
|
4月前
|
网络协议
【计算机网络-应用层】HTTP协议
【计算机网络-应用层】HTTP协议
|
4月前
|
网络协议 网络安全
【网络安全 | HTTP】 gopher协议原理、语法及利用总结
【网络安全 | HTTP】 gopher协议原理、语法及利用总结
83 0
|
1月前
|
安全 搜索推荐 数据安全/隐私保护
深入探讨HTTPS协议的原理和工作流程
【2月更文挑战第10天】
33 4
深入探讨HTTPS协议的原理和工作流程
|
2月前
|
安全 算法 Java
【JavaEE初阶】 详解HTTPS协议加密过程
【JavaEE初阶】 详解HTTPS协议加密过程
|
3月前
|
安全 算法 网络安全
浏览器 HTTPS 协议的相关知识点有哪些?
浏览器 HTTPS 协议的相关知识点有哪些?
20 0
|
3月前
|
缓存 前端开发
HTTP协议学习
HTTP协议学习
15 0
|
3月前
|
存储 缓存 安全
面试题:HTTP 协议包括哪些请求?
面试题:HTTP 协议包括哪些请求?
23 0
|
3月前
|
存储 安全 网络安全
HTTP与HTTPS的区别:安全性、协议地址和默认端口等比较
HTTP与HTTPS的区别:安全性、协议地址和默认端口等比较
142 0