📄 myhttpclient.h
字号:
* \param CodePage [in] A code page of the decoded string.
* \return The number of unicode characters required. (Not including a terminating NULL character)
* \throw Throws a httpclientexception if an error occurred.
*/
static inline DWORD UrlDecodeLen (PCWSTR szEncoded, BOOL bUtf8Encoding = FALSE, UINT CodePage = CP_ACP) throw (Exception &)
{
return UrlDecodeLenW (szEncoded, bUtf8Encoding, CodePage) ;
}
/*! \brief Decodes an URL-encoded string. */
static PSTR UrlDecodeA (PSTR szBuff, PCWSTR szEncoded, BOOL bUtf8Encoding = FALSE, UINT CodePage = CP_ACP) throw (Exception &) ;
/*! \brief Decodes an URL-encoded string. */
static PWSTR UrlDecodeW (PWSTR szBuff, PCWSTR szEncoded, BOOL bUtf8Encoding = FALSE, UINT CodePage = CP_ACP) throw (Exception &) ;
/*!
* \brief Decodes an URL-encoded string.
*
* This method decodes an URL-encoded string.
* This method does not support the URL-encoded string which contains a unicode character by using the %u or %x prefix.
* For more infomation about the Code-Page Identifiers, see the MSDN documentation.
*
* \param szBuff [out] A buffer to save the decoded string. The buffer can not be NULL.
* \param szEncoded [in] A string to decode.
* \param bUtf8Encoding [in] If this is TRUE, the decoded string is assumed an UTF-8 string.
* So the decoded string is converted into an Unicode string.
* \param CodePage [in] A code page of the decoded string.
* \return A decoded string.
* \throw Throws a httpclientexception if an error occurred.
*/
static inline PWSTR UrlDecode (PWSTR szBuff, PCWSTR szEncoded, BOOL bUtf8Encoding = FALSE, UINT CodePage = CP_ACP) throw (Exception &)
{
return UrlDecodeW (szBuff, szEncoded, bUtf8Encoding, CodePage) ;
}
private:
static void _Utf8CharToAnsiChar (PSTR szAnsiChar, PCSTR szUtf8Char, UINT CodePage = CP_ACP) throw (Exception &) ;
} ;
#ifdef UNICODE
typedef CHttpEncoderW CHttpEncoder ;
#else
typedef CHttpEncoderA CHttpEncoder ;
#endif
///////////////////////////////////////// CHttpEncoderT /////////////////////////////////////////
///////////////////////////////////////// CHttpResponseT /////////////////////////////////////////
/*!
* \brief This class represents a response returned by a HTTP web server.
*
* This class provides functionalities to handle a response which is returned by a HTTP web server.
* An instance of This class is returned if the request methods of the CHttpClientT class succeeds.
*
* \sa CHttpClientT, CHttpPostStatT
*
* The following code sample demonstrates the usage of the CHttpResponse class.
* \code
...
using namespace Ryeol ;
...
CHttpResponse * pobjHttpRes = NULL ;
try {
// Get the CHttpResponse object
pobjHttpRes = ... ;
// Reads the HTTP status code
_tprintf (_T ("%u"), pobjHttpRes->GetStatus ()) ;
// Reads the HTTP status text
_tprintf (_T (" %s\n"), pobjHttpRes->GetStatusText ()) ;
// Reads HTTP headers using an array of header names
static LPCTSTR szHeaders[] =
{ _T ("Server"), _T ("Date"), _T ("X-Powered-By"), _T ("Content-Length"), _T ("Set-Cookie")
, _T ("Expires"), _T ("Cache-control"), _T ("Connection"), _T ("Transfer-Encoding")
, _T ("Content-Type") } ;
LPCTSTR szHeader ;
for (size_t i = 0; i < sizeof (szHeaders) / sizeof (LPCTSTR); i++) {
if ( szHeader = pobjHttpRes->GetHeader (szHeaders[i]) )
_tprintf (_T ("%s: %s\r\n"), szHeaders[i], szHeader) ;
else
// If the header is not found..
_tprintf (_T ("'%s' header does not exist..\r\n"), szHeaders[i]) ;
}
_tprintf (_T ("\r\n")) ;
// Checks whether the returned stream is a text
BOOL bIsText = FALSE ;
if ( szHeader = pobjHttpRes->GetHeader (_T ("Content-Type")) )
bIsText = (0 == ::_tcsncicmp (szHeader, _T ("text/"), 5)) ;
// Reads the length of the stream
DWORD dwContSize ;
// If the length is not specified
if ( !pobjHttpRes->GetContentLength (dwContSize) )
dwContSize = 0 ;
const DWORD cbBuff = 1024 * 10 ;
BYTE byBuff[cbBuff] ;
DWORD dwRead ;
size_t cbTotal = 0 ;
// Reads the data stream returned by the HTTP server.
while ( dwRead = pobjHttpRes->ReadContent (byBuff, cbBuff - 1) ) {
cbTotal += dwRead ;
if ( bIsText ) {
byBuff[dwRead] = '\0' ;
printf ("%s", reinterpret_cast<LPCSTR> (byBuff)) ;
}
}
if ( !bIsText )
_tprintf (_T ("%u bytes skipped..\r\n"), cbTotal) ;
} catch (httpclientexception & e) {
_tprintf (_T ("An error has been occurred\n")) ;
_tprintf (_T ("ErrCode: 0x%x Win32ErrCode: 0x%x\n"), e.LastError (), e.Win32LastError ()) ;
_tprintf (_T ("ErrMsg: %s\n"), e.errmsg ()) ;
}
delete pobjHttpRes ;
pobjHttpRes = NULL ;
* \endcode
*/
template <typename HttpTool>
class CHttpResponseT
{
public:
// Basic type definitions ====================================================
typedef typename HttpTool::Exception Exception ; //!< typedef of httpclientexception
typedef typename HttpTool::CharType CharType ; //!< typedef of character type
typedef typename HttpTool::PSZ PSZ ; //!< typedef of null-terminated string
typedef typename HttpTool::PCSZ PCSZ ; //!< typedef of constant null-terminated string
/*! \brief Constructor with three internet handles */
CHttpResponseT (HINTERNET hInternet, HINTERNET hConnection, HINTERNET hRequest) throw () ;
/*! \brief Constructor with two internet handles */
CHttpResponseT (HINTERNET hConnection, HINTERNET hRequest) throw () ;
/*! \brief Constructor with one internet handles */
CHttpResponseT (HINTERNET hRequest) throw () ;
/*! \brief Default destructor */
virtual ~CHttpResponseT (void) throw () ;
/*! \brief Returns the number of headers of which name is szName */
DWORD GetHeaderCount (PCSZ szName) throw (Exception &) ;
/*! \brief Returns the header of which name is szName */
PCSZ GetHeader (PCSZ szName, DWORD nIdx = 0) throw (Exception &) ;
/*! \brief Returns the HTTP status code */
DWORD GetStatus (void) throw (Exception &) ;
/*! \brief Returns the HTTP status text */
PCSZ GetStatusText (void) throw (Exception &) ;
/*! \brief Retrieves the content length */
BOOL GetContentLength (DWORD & cbContLen) throw (Exception &) ;
/*! \brief Reads the content of a returned HTTP response */
DWORD ReadContent (BYTE * pbyBuff, DWORD cbBuff) throw (Exception &) ;
/*! \brief Saves the content of a returned HTTP response to a file */
void SaveContent (PCSZ szFilePath, BOOL bOverwrite = FALSE) throw (Exception &) ;
/*! \brief Returns the raw internet handle */
inline HINTERNET GetInternetHandle (void) const throw () { return m_hInternet ; }
/*! \brief Returns the raw connection handle */
inline HINTERNET GetConnectionHandle (void) const throw () { return m_hConnection ; }
/*! \brief Returns the raw request handle */
inline HINTERNET GetRequestHandle (void) const throw () { return m_hRequest ; }
private:
/*! \internal \brief Initializes the internal member variables */
void _Initialize (HINTERNET hInternet, HINTERNET hConnection, HINTERNET hRequest) throw () ;
/*! \internal \brief Caches HTTP headers whose name equal to the szName */
DWORD _LoadHeader (PCSZ szName) throw (Exception &) ;
CHttpClientMapT<HttpTool> m_mapHeader ; //!< Cached headers
HINTERNET m_hInternet ; //!< An internet handle
HINTERNET m_hConnection ; //!< A connection handle
HINTERNET m_hRequest ; //!< A request handle
DWORD m_dwStatus ; //!< A cached status code
PCSZ m_szStatusText ; //!< A cached status text
size_t m_cbContLen ; //!< A cached content-length
} ;
/*! \brief CHttpResponse class (Ansi version) */
typedef CHttpResponseT<CHttpToolA> CHttpResponseA ;
/*! \brief CHttpResponse class (Unicode version) */
typedef CHttpResponseT<CHttpToolW> CHttpResponseW ;
#ifdef UNICODE
/*! \brief CHttpResponse class (Generic type version) */
typedef CHttpResponseW CHttpResponse ;
#else
/*! \brief CHttpResponse class (Generic type version) */
typedef CHttpResponseA CHttpResponse ;
#endif
///////////////////////////////////////// CHttpResponseT /////////////////////////////////////////
///////////////////////////////////////// CHttpPostStatT /////////////////////////////////////////
/*!
* \brief This class represents progress information of the HTTP POST operation.
*
* The purpose of this class is to provide progress information to user.
* If you call BeginPost or BeginUpload method of the CHttpClient class, you can retrieve
* progress information by using the Query method of the CHttpClient class.
*
* \sa CHttpClientT, CHttpResponseT
*
* The following code sample demonstrates the usage of the CHttpPostStatT class.
* \code
...
using namespace Ryeol ;
...
CHttpClient objHttpReq ;
CHttpResponse * pobjHttpRes = NULL ;
size_t cbProceed = 1024 ; // 1k
try {
... ; // Intialize the CHttpClient object
// Starts a new POST request
objHttpReq.BeginPost (...) or objHttpReq.BeginUpload (...) ;
// Displays progress information
CHttpPostStat objPostStat ;
do {
// Retrieves progress information
objHttpReq.Query (objPostStat) ;
_tprintf (_T ("\nPost in progress... %2u/%2u\n")
, objPostStat.PostedCount () // The number of posted parameters
, objPostStat.TotalCount ()) ; // The total number of parameters
_tprintf (_T ("%s: %10u/%10u %10u/%10u %10u/%10u\n")
, objPostStat.CurrParam () // The name of the current parameter
, objPostStat.CurrParamPostedByte () // The number of posted bytes of the current parameter
, objPostStat.CurrParamTotalByte () // The total number of bytes of the current parameter
, objPostStat.PostedByte () // The number of posted bytes of the request
, objPostStat.TotalByte () // The total number of bytes of the request
, objPostStat.ActualPostedByte () // The actual number of posted bytes of the request
, objPostStat.ActualTotalByte ()) ; // The actual total number of bytes of the request
// If the current parameter is a file parameter, displays the file path
if ( objPostStat.CurrParamIsFile () )
_tprintf (_T ("-->%s\n")
, objPostStat.CurrFile ()) ;
// Sends the number of bytes specified by cbProceed to the server
} while ( !(pobjHttpRes = objHttpReq.Proceed (cbProceed)) ) ;
... ; // Handles the returned CHttpResponse object
} catch (httpclientexception & e) {
_tprintf (_T ("An error has been occurred\n")) ;
_tprintf (_T ("ErrCode: 0x%x Win32ErrCode: 0x%x\n"), e.LastError (), e.Win32LastError ()) ;
_tprintf (_T ("ErrMsg: %s\n"), e.errmsg ()) ;
}
...
* \endcode
*/
template <typename HttpTool>
class CHttpPostStatT {
public:
// Basic type definitions ====================================================
typedef typename HttpTool::Exception Exception ; //!< typedef of httpclientexception
typedef typename HttpTool::CharType CharType ; //!< typedef of character type
typedef typename HttpTool::PSZ PSZ ; //!< typedef of null-terminated string
typedef typename HttpTool::PCSZ PCSZ ; //!< typedef of constant null-terminated string
/*! \brief Default constructor */
CHttpPostStatT (void) throw () ;
/*! \brief Copy constructor */
CHttpPostStatT (const CHttpPostStatT & objPostStat) throw () ;
/*! \brief Default destructor */
virtual ~CHttpPostStatT (void) throw () ;
/*! \brief Indicates whether the POST is in progress or not */
BOOL IsActive (void) const throw () ;
/*! \brief Returns the actual number of bytes to send to a HTTP web server */
size_t ActualTotalByte (void) const throw (Exception &) ;
/*! \brief Returns the actual number of bytes posted to a HTTP web server */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -