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

📄 myhttpclient.h

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

	static inline LPSTR StringNCopy (LPSTR szDest, LPCSTR szSrc, size_t count) throw ()
	{
		return ::strncpy (szDest, szSrc, count) ;
	}

	static inline int StringNICmp (LPCSTR szStr1, LPCSTR szStr2, size_t count) throw ()
	{
		return ::_strnicmp (szStr1, szStr2, count) ;
	}

	static inline int StringCmp (LPCSTR szStr1, LPCSTR szStr2) throw ()
	{
		return ::strcmp (szStr1, szStr2) ;
	}

	static inline LPCSTR StringChr (LPCSTR szStr, int Chr) throw ()
	{
		return ::strchr (szStr, Chr) ;
	}

	static inline LPCSTR StringRChr (LPCSTR szStr, int Chr) throw ()
	{
		return ::strrchr (szStr, Chr) ;
	}

	static int SNPrintf (LPSTR buffer, size_t count, LPCSTR format, ...) throw ()
	{
		int			cchWritten ;
		va_list		args ;

		va_start (args, format) ;
		cchWritten = ::_vsnprintf (buffer, count, format, args) ;
		va_end (args) ;
		return cchWritten ;
	}

	static PCSTR StringStr (PCSTR szString, PCSTR szSearch) throw ()
	{
		return ::strstr (szString, szSearch) ;
	}

	static unsigned long StringToUL (PCSTR szString, PSTR * pEndPtr, int base) throw ()
	{
		return ::strtoul (szString, pEndPtr, base) ;
	}

	// Conversion methods
	static LPSTR Unicode2Ansi (LPCWSTR szStr, UINT CodePage = CP_ACP) throw (Exception &) ;
	static LPWSTR Ansi2Unicode (LPCSTR szStr, UINT CodePage = CP_ACP) throw (Exception &) ;

	// Methods for Ansi character set
	static BOOL IsDBCSLeadByteEx (UINT CodePage, CHAR TestChar) throw ()
	{
		return ::IsDBCSLeadByteEx (CodePage, TestChar) ;
	}

	// comparison functor operator (used by STL multimap)
	bool operator () (LPCSTR szKey1, LPCSTR szKey2) const throw () ;

	// Constants related to HTTP
	static LPCSTR							szDefUsrAgent ;
	static LPCSTR							szGET ;
	static LPCSTR							szPost ;
	static LPCSTR							szHTTP ;
	static LPCSTR							szHTTPS ;
	static LPCSTR							szSlash ;
	static LPCSTR							szCacheControl ;
	static LPCSTR							szNoCache ;
	static LPCSTR							szContentType ;
	static LPCSTR							szFormUrlEncoded ;
	static LPCSTR							szMultipartFormDataBoundary ;
	static LPCSTR							szDefBoundary ;
	static LPCSTR							szDefUploadContType ;
	static LPCSTR							szNULL ;
	static LPCSTR							szEmptyString ;
	static LPCSTR							szColonSlashSlash ;

	// Methods related to HTTP
	static HINTERNET OpenInternet (LPCSTR szUserAgent, DWORD dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG, LPCSTR szProxyName = NULL, LPCSTR szProxyBypass = NULL, DWORD dwFlags = 0) throw (Exception &) ;
	static void CloseInternet (HINTERNET & hInternet) throw () ;
	static HINTERNET OpenConnection (HINTERNET hInternet, LPCSTR szServerAddr, INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT, LPCSTR szUsrName = NULL, LPCSTR szUsrPwd = NULL) throw (Exception &) ;
	static void CloseConnection (HINTERNET & hConnection) throw () ;
	static HINTERNET OpenRequest (HINTERNET hConnection, LPCSTR szMethod, LPCSTR szObjectName, DWORD dwFlags = HTTPCLIENT_DEF_REQUEST_FLAGS_NOCACHE, LPCSTR szReferer = NULL, UINT CodePage = CP_ACP) throw (Exception &) ;
	static void CloseRequest (HINTERNET & hRequest) throw () ;
	static void AddHeader (HINTERNET hRequest, LPCSTR szName, LPCSTR szValue, UINT CodePage = CP_ACP) throw (Exception &) ;
	static void SendRequest (HINTERNET hRequest, LPCSTR szPosted = NULL, UINT CodePage = CP_ACP) throw (Exception &) ;
	static void SendRequestEx (HINTERNET hRequest, DWORD dwPostedSize) throw (Exception &) ;
	static void InternetWriteFile (HINTERNET hRequest, const BYTE * pbyBuff, DWORD cbyBuff) throw (Exception &) ;
	static void EndRequest (HINTERNET hRequest) throw (Exception &) ;
	static BOOL FileExists (LPCSTR szFilePath) throw (Exception &) ;
	static HANDLE OpenFile (LPCSTR szFilePath) throw (Exception &) ;
	static HANDLE CreateFileAlwaysToWrite (LPCSTR szFilePath) throw (Exception &) ;
	static DWORD GetFileSize (HANDLE hFile, LPCSTR szFilePath) throw (Exception &) ;
	static LPSTR GetMimeType (HANDLE hFile, UINT CodePage = CP_ACP) throw (Exception &) ;
	static LPSTR GetStatusText (HINTERNET hRequest) throw (Exception &) ;
	static LPSTR GetHeader (HINTERNET hRequest, LPCSTR szName, DWORD * pnIdx = NULL) throw (Exception &) ;
	static void InternetSetOption (HINTERNET hInternet, DWORD dwOption, LPVOID lpBuffer, DWORD dwBufferLength) throw (Exception &) ;
	static LPSTR CreateUploadBoundary (void) throw () ;
} ;
///////////////////////////////////////// CHttpToolA /////////////////////////////////////////


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

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

	// String type definitions =======================================================
	typedef WCHAR				CharType ;
	typedef LPWSTR				PSZ ;
	typedef LPCWSTR				PCSZ ;

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

	static inline size_t StringLen (LPCWSTR szStr) throw ()
	{
		return ::wcslen (szStr) ;
	}

	static inline LPWSTR StringCopy (LPWSTR szDest, LPCWSTR szSrc) throw ()
	{
		return ::wcscpy (szDest, szSrc) ;
	}

	static inline LPWSTR StringCat (LPWSTR szDest, LPCWSTR szSrc) throw ()
	{
		return ::wcscat (szDest, szSrc) ;
	}

	static inline LPWSTR StringNCopy (LPWSTR szDest, LPCWSTR szSrc, size_t count) throw ()
	{
		return ::wcsncpy (szDest, szSrc, count) ;
	}

	static inline int StringNICmp (LPCWSTR szStr1, LPCWSTR szStr2, size_t count) throw ()
	{
		return ::_wcsnicmp (szStr1, szStr2, count) ;
	}

	static inline int StringCmp (LPCWSTR szStr1, LPCWSTR szStr2) throw ()
	{
		return ::wcscmp (szStr1, szStr2) ;
	}

	static inline LPCWSTR StringChr (LPCWSTR szStr, WCHAR Chr) throw ()
	{
		return ::wcschr (szStr, Chr) ;
	}

	static inline LPCWSTR StringRChr (LPCWSTR szStr, WCHAR Chr) throw ()
	{
		return ::wcsrchr (szStr, Chr) ;
	}

	static int SNPrintf (LPWSTR buffer, size_t count, LPCWSTR format, ...) throw ()
	{
		int			cchWritten ;
		va_list		args ;

		va_start (args, format) ;
		cchWritten = ::_vsnwprintf (buffer, count, format, args) ;
		va_end (args) ;
		return cchWritten ;
	}

	static PCWSTR StringStr (PCWSTR szString, PCWSTR szSearch) throw ()
	{
		return ::wcsstr (szString, szSearch) ;
	}

	static unsigned long StringToUL (PCWSTR szString, PWSTR * pEndPtr, int base) throw ()
	{
		return ::wcstoul (szString, pEndPtr, base) ;
	}

	// Conversion methods
	static LPSTR Unicode2Ansi (LPCWSTR szStr, UINT CodePage = CP_ACP) throw (Exception &) ;
	static LPWSTR Ansi2Unicode (LPCSTR szStr, UINT CodePage = CP_ACP) throw (Exception &) ;

	// Methods for Ansi character set
	static BOOL IsDBCSLeadByteEx (UINT /* CodePage */, WCHAR /* TestChar */) throw ()
	{
		return FALSE ;		// always returns FALSE
	}

	// comparison functor operator (used by STL multimap)
	bool operator () (LPCWSTR szKey1, LPCWSTR szKey2) const throw () ;

	// Constants related to HTTP
	static LPCWSTR							szDefUsrAgent ;
	static LPCWSTR							szGET ;
	static LPCWSTR							szPost ;
	static LPCWSTR							szHTTP ;
	static LPCWSTR							szHTTPS ;
	static LPCWSTR							szSlash ;
	static LPCWSTR							szCacheControl ;
	static LPCWSTR							szNoCache ;
	static LPCWSTR							szContentType ;
	static LPCWSTR							szFormUrlEncoded ;
	static LPCWSTR							szMultipartFormDataBoundary ;
	static LPCWSTR							szDefBoundary ;
	static LPCWSTR							szDefUploadContType ;
	static LPCWSTR							szNULL ;
	static LPCWSTR							szEmptyString ;
	static LPCWSTR							szColonSlashSlash ;

	// Methods related to HTTP
	static HINTERNET OpenInternet (LPCWSTR szUserAgent, DWORD dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG, LPCWSTR szProxyName = NULL, LPCWSTR szProxyBypass = NULL, DWORD dwFlags = 0) throw (Exception &) ;
	static void CloseInternet (HINTERNET & hInternet) throw () ;
	static HINTERNET OpenConnection (HINTERNET hInternet, LPCWSTR szServerAddr, INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT, LPCWSTR szUsrName = NULL, LPCWSTR szUsrPwd = NULL) throw (Exception &) ;
	static void CloseConnection (HINTERNET & hConnection) throw () ;
	static HINTERNET OpenRequest (HINTERNET hConnection, LPCWSTR szMethod, LPCWSTR szObjectName, DWORD dwFlags = HTTPCLIENT_DEF_REQUEST_FLAGS_NOCACHE, LPCWSTR szReferer = NULL, UINT CodePage = CP_ACP) throw (Exception &) ;
	static void CloseRequest (HINTERNET & hRequest) throw () ;
	static void AddHeader (HINTERNET hRequest, LPCWSTR szName, LPCWSTR szValue, UINT CodePage = CP_ACP) throw (Exception &) ;
	static void SendRequest (HINTERNET hRequest, LPCWSTR szPosted = NULL, UINT CodePage = CP_ACP) throw (Exception &) ;
	static void SendRequestEx (HINTERNET hRequest, DWORD dwPostedSize) throw (Exception &) ;
	static void InternetWriteFile (HINTERNET hRequest, const BYTE * pbyBuff, DWORD cbyBuff) throw (Exception &) ;
	static void EndRequest (HINTERNET hRequest) throw (Exception &) ;
	static BOOL FileExists (LPCWSTR szFilePath) throw (Exception &) ;
	static HANDLE OpenFile (LPCWSTR szFilePath) throw (Exception &) ;
	static HANDLE CreateFileAlwaysToWrite (LPCWSTR szFilePath) throw (Exception &) ;
	static DWORD GetFileSize (HANDLE hFile, LPCWSTR szFilePath) throw (Exception &) ;
	static LPSTR GetMimeType (HANDLE hFile, UINT CodePage = CP_ACP) throw (Exception &) ;
	static LPWSTR GetStatusText (HINTERNET hRequest) throw (Exception &) ;
	static LPWSTR GetHeader (HINTERNET hRequest, LPCWSTR szName, DWORD * pnIdx = NULL) throw (Exception &) ;
	static void InternetSetOption (HINTERNET hInternet, DWORD dwOption, LPVOID lpBuffer, DWORD dwBufferLength) throw (Exception &) ;
	static LPWSTR CreateUploadBoundary (void) throw () ;
} ;
///////////////////////////////////////// CHttpToolW /////////////////////////////////////////


///////////////////////////////////////// CHttpClientMapT /////////////////////////////////////////
/*!
 * \internal
 * \brief	This is a private map for classes in this file.
 *
 * This is a private map for classes in this file. It uses STL's multimap.
 * If a method's return value is a string or contains a string, it guarantees that
 * it does not return NULL for string. (It returns "" instead of NULL)
 * It returns NULL only if an error occurred or the specified element is not found.
 */
template <typename HttpTool>
class CHttpClientMapT
{
public:
	// Basic type definitions ====================================================
	typedef typename HttpTool::Exception		Exception ;
	typedef typename HttpTool::CharType			CharType ;
	typedef typename HttpTool::PSZ				PSZ ;
	typedef typename HttpTool::PCSZ				PCSZ ;

	CHttpClientMapT (void) throw () ;
	virtual ~CHttpClientMapT (void) throw () ;

	// MapValue
	typedef struct _MapValue {
		PCSZ		szValue ;
		DWORD		dwFlag ;
		void Delete (void) throw () {
			::free ((void *) szValue) ;
			szValue = NULL ;
		}
	} MapValue ;

	typedef typename std::multimap<PCSZ, MapValue, HttpTool>::const_iterator	ConstMapIter ;

	BOOL Clear (void) throw () ;
	BOOL Remove (DWORD nIdx) throw () ;
	BOOL RemoveAll (PCSZ szName) throw (Exception &) ;
	BOOL Remove (PCSZ szName, DWORD nIdx = 0) throw (Exception &) ;

	BOOL Exists (PCSZ szName, DWORD nIdx = 0) throw (Exception &) ;
	DWORD Count (PCSZ szName = NULL) throw () ;
	BOOL Empty (void) const throw () ;

⌨️ 快捷键说明

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