⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpdownload.h

📁 基于Winsock2的支持断点续传和SOCKS代理的HTTP下载类 CHttpDownload
💻 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 + -