?? httpdownload.h
字號:
// HttpDownload.h: interface for the CHttpDownload class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_HTTPDOWNLOAD_H__11AB7F1F_62EA_47FC_8B2D_B2E3929E2861__INCLUDED_)
#define AFX_HTTPDOWNLOAD_H__11AB7F1F_62EA_47FC_8B2D_B2E3929E2861__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// 結構定義
typedef struct _tagDownloadStatus
{
UINT nStatusType;
DWORD dwFileSize;
DWORD dwFileDownloadedSize;
CString strFileName;
// UINT nErrorCount;
// CString strError;
// DWORD dwErrorCode;
}DOWNLOADSTATUS,*PDOWNLOADSTATUS;
// 常量定義
// 缺省超時參數
const DWORD DOWNLOAD_CONNECT_TIMEOUT= 10*1000;// 10秒
const DWORD DOWNLOAD_SEND_TIMEOUT = 10*1000;// 10秒
const DWORD DOWNLOAD_RECV_TIMEOUT = 10*1000;// 10秒
// 下載結果
const UINT DOWNLOAD_RESULT_SUCCESS = 0; // 成功
const UINT DOWNLOAD_RESULT_SAMEAS = 1; // 要下載的文件已經存在并且與遠程文件一致,不用下載
const UINT DOWNLOAD_RESULT_STOP = 2; // 中途停止(用戶中斷)
const UINT DOWNLOAD_RESULT_FAIL = 3; // 下載失敗
// 發送請求
const UINT SENDREQUEST_SUCCESS = 0; // 成功
const UINT SENDREQUEST_ERROR = 1; // 一般網絡錯誤,可以重試
const UINT SENDREQUEST_STOP = 2; // 中途停止(用戶中斷) (不用重試)
const UINT SENDREQUEST_FAIL = 3; // 失敗 (不用重試)
// 消息
const WPARAM MSG_INTERNET_STATUS = (WPARAM)1;
const WPARAM MSG_DOWNLOAD_STATUS = (WPARAM)2;
const WPARAM MSG_DOWNLOAD_RESULT = (WPARAM)3;
const WPARAM MSG_DOWNLOAD_MAX = (WPARAM)32; //保留最大可供擴充
//下載狀態
const UINT STATUS_TYPE_FILENAME = 1;
const UINT STATUS_TYPE_FILESIZE = 2;
const UINT STATUS_TYPE_FILEDOWNLOADEDSIZE = 3;
const UINT STATUS_TYPE_ERROR_COUNT = 4;
const UINT STATUS_TYPE_ERROR_CODE = 5;
const UINT STATUS_TYPE_ERROR_STRING = 6;
// 重試類別
const UINT RETRY_TYPE_NONE = 0;
const UINT RETRY_TYPE_TIMES = 1;
const UINT RETRY_TYPE_ALWAYS= 2;
//缺省的重試次數
const UINT DEFAULT_RETRY_MAX= 10;
// 缺省端口號
const USHORT DEFAULT_HTTP_PORT = 80;
const USHORT DEFAULT_HTTPS_PORT= 443;
const USHORT DEFAULT_FTP_PORT = 21;
const USHORT DEFAULT_SOCKS_PORT= 1080;
// HTTP STATUS CODE分類
const UINT HTTP_OK = 0;
const UINT HTTP_ERROR = 1;
const UINT HTTP_REDIRECT = 2;
const UINT HTTP_FAIL = 3;
// PROXY的類型
const UINT PROXY_NONE = 0;
const UINT PROXY_HTTPGET = 1;
const UINT PROXY_HTTPCONNECT = 2;
const UINT PROXY_SOCKS4 = 3;
const UINT PROXY_SOCKS4A = 4;
const UINT PROXY_SOCKS5 = 5;
class CHttpDownload
{
public:
CHttpDownload();
virtual ~CHttpDownload();
public:
// 靜態函數,用于64編碼、解碼
static int Base64Encode( LPCTSTR lpszEncoding, CString& strEncoded );
static int Base64Decode( LPCTSTR lpszDecoding, CString& strDecoded );
// DWONLOAD函數
void SetAuthorization(LPCTSTR lpszUsername,LPCTSTR lpszPassword,BOOL bAuthorization = TRUE );
void SetReferer(LPCTSTR lpszReferer);
void SetUserAgent(LPCTSTR lpszUserAgent);
void SetTimeout(DWORD dwSendTimeout,DWORD dwReceiveTimeout,DWORD dwConnectTimeout);
void SetRetry(UINT nRetryType,UINT nRetryDelay=0,UINT nRetryMax = 0);
void SetNotifyWnd(HWND hNotifyWnd,UINT nNotifyMsg,BOOL bNotify = TRUE );
void SetProxy(LPCTSTR lpszProxyServer,USHORT nProxyPort,BOOL bProxy=TRUE,BOOL bProxyAuthorization = FALSE,LPCTSTR lpszProxyUsername = NULL,LPCTSTR lpszProxyPassword = NULL,UINT nProxyType = PROXY_HTTPGET);
void StopDownload();
BOOL ParseURL(LPCTSTR lpszURL,CString& strServer,CString& strObject,USHORT& nPort);
BOOL GetDownloadFileStatus(LPCTSTR lpszDownloadUrl,DWORD &dwFileSize,CTime &FileTime);
UINT Download(LPCTSTR lpszDownloadUrl,LPCTSTR lpszSavePath,BOOL bForceDownload = FALSE);
private:
UINT SendRequest(BOOL bHead = FALSE);
BOOL CreateSocket();
void CloseSocket();
UINT GetInfo(LPCTSTR lpszHeader,DWORD& dwContentLength,DWORD& dwStatusCode,CTime& TimeLastModified);
CTime GetTime(LPCTSTR lpszTime);
private:
// 下載參數
// 待下載URL和本地保存路徑
CString m_strDownloadUrl;
CString m_strSavePath;
CString m_strTempSavePath;//臨時文件的路徑
// 停止下載
BOOL m_bStopDownload;
// 強制重新下載(不管已有的文件是否與遠程文件相同)
BOOL m_bForceDownload;
// 是否支持斷點續傳
BOOL m_bSupportResume;
// 文件以及下載大小
DWORD m_dwFileSize; // 文件總的大小
DWORD m_dwFileDownloadedSize; // 文件總共已經下載的大小
DWORD m_dwDownloadSize; // 本次需要下載的大小
DWORD m_dwDownloadedSize; // 本次已經下載的大小
DWORD m_dwHeaderSize; // 返回頭的大小
CString m_strHeader; // 保存頭部信息
// 文件日期(遠程文件的信息)
CTime m_TimeLastModified;
// Referer
CString m_strReferer;
// UserAgent
CString m_strUserAgent;
// 超時TIMEOUT 連接超時、發送超時、接收超時(單位:毫秒)
DWORD m_dwConnectTimeout;
DWORD m_dwReceiveTimeout;
DWORD m_dwSendTimeout;
// 重試機制
UINT m_nRetryType; //重試類型(0:不重試 1:重試一定次數 2:總是重試)
UINT m_nRetryTimes; //重試次數
UINT m_nRetryDelay; //重試延遲(單位:毫秒)
UINT m_nRetryMax; //重試的最大次數
// 錯誤處理
UINT m_nErrorCount; //錯誤次數
CString m_strError; //錯誤信息
// 向其他窗口發送消息
BOOL m_bNotify; // 是否向外發送通知消息
HWND m_hNotifyWnd; // 被通知的窗口
UINT m_nNotifyMessage; // 被通知的消息
// 是否進行驗證 : Request-Header: Authorization
BOOL m_bAuthorization;
CString m_strUsername;
CString m_strPassword;
// 是否使用代理
BOOL m_bProxy;
CString m_strProxyServer;
USHORT m_nProxyPort;
UINT m_nProxyType;
// 代理是否需要驗證: Request-Header: Proxy-Authorization
BOOL m_bProxyAuthorization;
CString m_strProxyUsername;
CString m_strProxyPassword;
// 下載過程中所用的變量
CString m_strServer;
CString m_strObject;
CString m_strFileName;
USHORT m_nPort;
SOCKET m_hSocket; // 下載連接的SOCKET
PBSD m_pBSD; // BufSocketData結構
// 用于BASE64編碼、解碼
static UINT m_nBase64Mask[];
static CString m_strBase64TAB;
};
#endif // !defined(AFX_HTTPDOWNLOAD_H__11AB7F1F_62EA_47FC_8B2D_B2E3929E2861__INCLUDED_)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -