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

📄 myhttpclient.h

📁 自定义HttpClient类
💻 H
📖 第 1 页 / 共 5 页
字号:
#pragma once

#include <windows.h>			// for generic types, .. etc
#include <wininet.h>			// for the windows internet functions
#include <crtdbg.h>				// for the _ASSERTE macro
#include <objbase.h>			// for ::CoCreateGuid

#include <utility>				// for the STL pair type
#include <map>					// for the STL multimap container
#include <vector>				// for the STL vector container

#include "MyException.h"		// for exception classes
#include "SafeInt.hpp"			// for the SaftInt class

#pragma warning (push)
#pragma warning (disable: 4290)	// avoids 'C++ Exception Specification ignored' message
#pragma warning (disable: 4996)	// avoids 'This function or variable may be unsafe' message

/*!
 * \brief	The namespace of the Ryeol's library
 *
 * This is the namespace for source codes written by Jo Hyeong-ryeol.
 */
namespace Ryeol {

///////////////////////////////////////// Global constant definitions /////////////////////////////////////////
/*!
 * \brief	Default flags used by the CHttpClient class to open a HTTP request
 * 
 * These flags are default flags used by the CHttpClient to open a HTTP request. These flags
 * are actually the dwFlags parameter of the ::HttpOpenRequest function of the WinInet API.
 * For more detailed information about this flags, see microsoft's SDK documentation.
 *
 * \sa CHttpClientT<HttpTool, HttpEncoder>::RequestGetEx
 * \sa CHttpClientT<HttpTool, HttpEncoder>::BeginPostEx
 * \sa CHttpClientT<HttpTool, HttpEncoder>::BeginUploadEx
 * \sa CHttpClientT<HttpTool, HttpEncoder>::RequestPostEx
 * \sa CHttpClientT<HttpTool, HttpEncoder>::RequestUploadEx
 */
enum HttpClientDefFlag
{
	HTTPCLIENT_DEF_REQUEST_FLAGS				= INTERNET_FLAG_HYPERLINK
												| INTERNET_FLAG_KEEP_CONNECTION
												| INTERNET_FLAG_NO_UI
												| INTERNET_FLAG_RESYNCHRONIZE
	//!< The default flag which causes the CHttpClient to use the cache if a cached copy exists.

	, HTTPCLIENT_DEF_REQUEST_FLAGS_NOCACHE		= INTERNET_FLAG_HYPERLINK
												| INTERNET_FLAG_KEEP_CONNECTION
												| INTERNET_FLAG_NO_UI
												| INTERNET_FLAG_RESYNCHRONIZE
												| INTERNET_FLAG_NO_CACHE_WRITE
												| INTERNET_FLAG_PRAGMA_NOCACHE
												| INTERNET_FLAG_RELOAD
	//!< The default flag which causes the CHttpClient not to use the cache.
} ;


/*!
 * \brief	These error codes represent the error occurred while processing an operation.
 * 
 * These error codes are custom error codes only for classes in this file.
 */
enum HttpClientErrorCode
{
	HTTPCLIENT_ERR_NOT_SPECIFIED						= 0		//!< Error was not occurred or not specified.

	// Normal error
	, HTTPCLIENT_ERR_UNEXPECTED_ERROR					= 100	//!< Unknown error occurred.
	, HTTPCLIENT_ERR_OUT_OF_RANGE						= 101	//!< The index is out of range.
	, HTTPCLIENT_ERR_OUT_OF_MEMORY						= 102	//!< The memory has been exhausted.
	, HTTPCLIENT_ERR_INVALID_URL						= 103	//!< The requested URL is not a valid URL.
	, HTTPCLIENT_ERR_POST_NOT_STARTED					= 104	//!< The post context is not started yet.
	, HTTPCLIENT_ERR_READ_UNEXPECTED_SIZE				= 105	//!< Couldn't read expected bytes from a file.
	, HTTPCLIENT_ERR_POST_NOT_FINISHED					= 106	//!< The post context has not been finished yet.
	, HTTPCLIENT_ERR_INTERNET_PORT_NOT_VALID			= 107	//!< The port number is not valid.
	, HTTPCLIENT_ERR_STD_EXCEPTION						= 108	//!< std::exception occurred.
	, HTTPCLIENT_ERR_ENCODED_URL_NOT_VALID				= 109	//!< The encoded URL is not valid.
	, HTTPCLIENT_ERR_INVALID_UTF8_CHARACTER				= 110	//!< The UTF8 string contains an invalid character.
	, HTTPCLIENT_ERR_UNEXPECTED_ARITHMETIC_ERROR		= 111	//!< An unexpected arithmetic error has been occurred.
	, HTTPCLIENT_ERR_ARITHMETIC_OVERFLOW				= 112	//!< An arithmetic overflow error has been occurred.
	, HTTPCLIENT_ERR_INT_DIVIDE_BY_ZERO					= 113	//!< An interger divide by zero exception has been occurred.
	, HTTPCLIENT_ERR_FILE_ALEADY_EXISTS					= 114	//!< A file aleady exists. So it doesn't overwrite it.


	// Normal error (which has a win32 error code) - Reserved


	// WinInet error (which has a win32 error code)
	, HTTPCLIENT_ERR_QUERYINFO_FAILED					= 400	//!< ::HttpQueryInfo failed.
	, HTTPCLIENT_ERR_INTERNETREADFILE_FAILED			= 401	//!< ::InternetReadFile failed.
	, HTTPCLIENT_ERR_INTERNETOPEN_FAILED				= 402	//!< ::InternetOpen failed.
	, HTTPCLIENT_ERR_INTERNETCONNECT_FAILED				= 403	//!< ::InternetConnect failed.
	, HTTPCLIENT_ERR_HTTPOPENREQUEST_FAILED				= 404	//!< ::HttpOpenRequest failed.
	, HTTPCLIENT_ERR_HTTPADDREQUESTHEADERS_FAILED		= 405	//!< ::HttpAddRequestHeaders failed.
	, HTTPCLIENT_ERR_HTTPSENDREQUEST_FAILED				= 406	//!< ::HttpSendRequest failed.
	, HTTPCLIENT_ERR_HTTPSENDREQUESTEX_FAILED			= 407	//!< ::HttpSendRequestEx failed.
	, HTTPCLIENT_ERR_INTERNETWRITEFILE_FAILED			= 408	//!< ::InternetWriteFile failed.
	, HTTPCLIENT_ERR_HTTPENDREQUEST_FAILED				= 409	//!< ::HttpEndRequest failed.
	, HTTPCLIENT_ERR_INTERNETSETOPTION_FAILED			= 410	//!< ::InternetSetOption failed.

	// Win32 API error (which has a win32 error code)
	, HTTPCLIENT_ERR_WIDECHARTOMULTIBYTE_FAILED			= 600	//!< ::WideCharToMultiByte failed.
	, HTTPCLIENT_ERR_MULTIBYTETOWIDECHAR_FAILED			= 601	//!< ::MultiByteToWideChar failed.
	, HTTPCLIENT_ERR_READFILE_FAILED					= 602	//!< ::ReadFile failed.
	, HTTPCLIENT_ERR_OPENFILE_FAILED					= 603	//!< OpenFile (::CreateFile) failed.
	, HTTPCLIENT_ERR_SETFILEPOINTER_FAILED				= 604	//!< ::SetFilePointer failed.
	, HTTPCLIENT_ERR_GETFILESIZE_FAILED					= 605	//!< ::GetFileSize failed.
	, HTTPCLIENT_ERR_WRITEFILE_FAILED					= 606	//!< ::WriteFile failed.

	// user-defined error
	, HTTPCLIENT_ERR_USER								= 1000	//!< Beginning of the user-defined error code.
																//! \nThe maximum value is HTTPCLIENT_ERR_USER + 99.
} ;

template <typename HttpTool, typename HttpEncoder> class CHttpClientT ;

///////////////////////////////////////// Global constant definitions /////////////////////////////////////////

///////////////////////////////////////// httpclientexception /////////////////////////////////////////
/*!
 * \brief	The standard exception class used by classes in this file. (Ansi Ver.)
 *
 * This class represents an exception occurred in HTTP client classes. All classes in this file
 * will throw this class when an error has been occurred.
 */
class httpclientexceptionA : public errmsg_exceptionA {
public:
	/*! \brief	Default constructor */
	httpclientexceptionA (void) throw () ;
	/*! \brief	Constructor with initial arguments */
	httpclientexceptionA (LPCSTR szErrMsg, DWORD dwLastError = HTTPCLIENT_ERR_NOT_SPECIFIED, DWORD dwWin32LastError = NO_ERROR) throw () ;

	/*! \brief	Returns last error code
	 *
	 * This error code represents the error occurred in classes of this file.
	 */
	inline DWORD LastError (void) const throw ()
	{
		return m_dwLastError ;
	}

	/*! \brief	Sets the last error code
	 *
	 * This error code represents the error occurred in classes of this file.
	 */
	inline void SetLastError (DWORD dwErrCode) throw ()
	{
		m_dwLastError = dwErrCode ;
	}

	/*! \brief	Returns last win32 error code
	 *
	 * Returns the last win32 error code retrieved by using ::GetLastError when an error occurred.
	 */
	inline DWORD Win32LastError (void) const throw ()
	{
		return m_dwWin32LastError ;
	}

	/*! \brief	Sets the last win32 error code
	 *
	 * This method sets the last win32 error code retrieved by using ::GetLastError.
	 */
	inline void SetWin32LastError (DWORD dwErrCode) throw ()
	{
		m_dwWin32LastError = dwErrCode ;
	}

private:
	DWORD			m_dwLastError ;			//!< The last error code.
	DWORD			m_dwWin32LastError ;	//!< The last win32 error code.
} ;

/*!
 * \brief	The standard exception class used by classes in this file. (Unicode Ver.)
 *
 * This class represents an exception occurred in HTTP client classes. All classes in this file
 * will throw this class when an error has been occurred.
 */
class httpclientexceptionW : public errmsg_exceptionW {
public:
	/*! \brief	Default constructor */
	httpclientexceptionW (void) throw () ;
	/*! \brief	Constructor with initial arguments */
	httpclientexceptionW (LPCWSTR szErrMsg, DWORD dwLastError = HTTPCLIENT_ERR_NOT_SPECIFIED, DWORD dwWin32LastError = NO_ERROR) throw () ;

	/*!
	 * \brief	Returns "httpclientexceptionW"
	 *
	 * This is not supported in Unicode version.
	 * It always returns "httpclientexceptionW".
	 *
	 * \return				"httpclientexceptionW"
	 */
	inline LPCSTR what (void) const throw ()
	{
		return "httpclientexceptionW" ;
	}

	/*! \brief	Returns last error code
	 *
	 * This error code represents the error occurred in classes of this file.
	 */
	inline DWORD LastError (void) const throw ()
	{
		return m_dwLastError ;
	}

	/*! \brief	Sets the last error code
	 *
	 * This error code represents the error occurred in classes of this file.
	 */
	inline void SetLastError (DWORD dwErrCode) throw ()
	{
		m_dwLastError = dwErrCode ;
	}

	/*! \brief	Returns last win32 error code
	 *
	 * Returns the last win32 error code retrieved by using ::GetLastError when an error occurred.
	 */
	inline DWORD Win32LastError (void) const throw ()
	{
		return m_dwWin32LastError ;
	}

	/*! \brief	Sets the last win32 error code
	 *
	 * This method sets the last win32 error code retrieved by using ::GetLastError.
	 */
	inline void SetWin32LastError (DWORD dwErrCode) throw ()
	{
		m_dwWin32LastError = dwErrCode ;
	}

private:
	DWORD			m_dwLastError ;			//!< The last error code.
	DWORD			m_dwWin32LastError ;	//!< The last win32 error code.
} ;

#ifdef UNICODE
	typedef httpclientexceptionW		httpclientexception ;
#else
	typedef httpclientexceptionA		httpclientexception ;
#endif

///////////////////////////////////////// httpclientexception /////////////////////////////////////////


///////////////////////////////////////// CHttpToolA /////////////////////////////////////////
/*!
 * \internal
 * \brief	This class contains utility methods. (Ansi Ver.)
 *
 * This class provides some utility methods and gives character type independence.
 * (Internal use only)
 */
class CHttpToolA
{
public:
	// Returns constant messages
	static inline LPCSTR GetConstMessage (DWORD nIdx) throw () ;

	// Methods related to the exception
	typedef	httpclientexceptionA			Exception ;
	static void ThrowException (DWORD nErrMsgIdx) throw (Exception &) ;
	static void ThrowException (LPCSTR szErrMsg, DWORD nErrMsgIdx = HTTPCLIENT_ERR_NOT_SPECIFIED) throw (Exception &) ;
	static void ThrowException (DWORD nErrMsgIdx, DWORD dwErrCode, LPCSTR szStrArg = NULL) throw (Exception &) ;
	static void ThrowException (LPCWSTR szErrMsg, DWORD nErrMsgIdx = HTTPCLIENT_ERR_NOT_SPECIFIED, DWORD dwErrCode = NO_ERROR) throw (Exception &) ;
	static void ThrowException (httpclientexceptionW & e) throw (Exception &) ;
	static void ThrowException (::SafeIntException & e) throw (Exception &) ;

	// String type definitions =======================================================
	typedef CHAR				CharType ;
	typedef LPSTR				PSZ ;
	typedef LPCSTR				PCSZ ;

	static inline BOOL IsAnsi (void) throw ()
	{
		return TRUE ;
	}

	// Wrapper methods for CRT string functions
	static inline size_t StringLen (LPCSTR szStr) throw ()
	{
		return ::strlen (szStr) ;
	}

	static inline LPSTR StringCopy (LPSTR szDest, LPCSTR szSrc) throw ()
	{
		return ::strcpy (szDest, szSrc) ;
	}

	static inline LPSTR StringCat (LPSTR szDest, LPCSTR szSrc) throw ()
	{
		return ::strcat (szDest, szSrc) ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -