Click here to Skip to main content
15,893,401 members
Articles / Desktop Programming / MFC

CHttpClient - A Helper Class Using WinInet

Rate me:
Please Sign up or sign in to vote.
4.96/5 (59 votes)
10 Aug 20073 min read 487.9K   24.9K   163  
A C++ class which helps you to interact with a HTTP web server.
/*!
 * \file	HttpClient.h
 * \brief	A header file of the HttpClient component.
 * \author	Jo Hyeong-ryeol
 * \since	2004.10.17
 * \version	$LastChangedRevision: 93 $
 *			$LastChangedDate: 2006-01-30 19:13:44 +0900 (월, 30 1 2006) $
 * 
 * This file is a header file of the HttpClient component.
 * \n\n
 * Copyright &copy; 2006 by <a href="mailto:hyeongryeol@gmail.com">Jo Hyeong-ryeol</a>\n
 * Permission to copy, use, modify, sell and distribute this software is
 * granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied warranty,
 * and with no claim as to its suitability for any purpose.
 */
#pragma once
#include "resource.h"

#include "RyeolHttpClientCom.h"

// CHttpClient

class ATL_NO_VTABLE CHttpClient : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CHttpClient, &CLSID_HttpClient2>,
	public ISupportErrorInfo,
	public IHttpClient2,
	public IDispatchImpl<IDispHttpClient2, &IID_IDispHttpClient2, &LIBID_RyeolHttpClientCom2Lib>
{
public:
	CHttpClient()
	{
	}

DECLARE_REGISTRY_RESOURCEID(IDR_HTTPCLIENT)

BEGIN_CATEGORY_MAP(CHttpClient)
	IMPLEMENTED_CATEGORY(CATID_SafeForScripting)
	IMPLEMENTED_CATEGORY(CATID_SafeForInitializing)
END_CATEGORY_MAP()

BEGIN_COM_MAP(CHttpClient)
	COM_INTERFACE_ENTRY(IDispHttpClient2)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(IHttpClient2)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()

// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}
	
	void FinalRelease() 
	{
	}

	// HttpClient error methods
	IMPLEMENT_HTTPCLIENT_ERROR_HANDLER()

public:

	#define DECLARE_HTTPCLIENT_STRINGPROPERTY_READONLY(PropertyName)							\
																								\
	STDMETHOD(Get##PropertyName##Len)(DWORD * pdwLen) ;											\
	STDMETHOD(Get##PropertyName##IntoBuff)(OLECHAR * szBuff, DWORD cchBuff) ;					\
	STDMETHOD(Get##PropertyName)(BSTR * pbstr##PropertyName) ;									\
	STDMETHOD(get_##PropertyName)(BSTR * pbstr##PropertyName) ;


	#define DECLARE_HTTPCLIENT_STRINGPROPERTY(PropertyName)										\
																								\
	DECLARE_HTTPCLIENT_STRINGPROPERTY_READONLY (PropertyName)									\
	STDMETHOD(Set##PropertyName)(PCWSTR sz##PropertyName) ;										\
	STDMETHOD(put_##PropertyName)(const BSTR bstr##PropertyName) ;


	#define DECLARE_HTTPCLIENT_VALUEPROPERTY(ValueType, PropertyName)							\
																								\
	STDMETHOD(Get##PropertyName)(ValueType * p##PropertyName) ;									\
	STDMETHOD(Set##PropertyName)(ValueType PropertyName) ;										\
	STDMETHOD(get_##PropertyName)(ValueType * p##PropertyName) ;								\
	STDMETHOD(put_##PropertyName)(ValueType PropertyName) ;

	
	DECLARE_HTTPCLIENT_VALUEPROPERTY (BOOL, StrictFileCheck)
	DECLARE_HTTPCLIENT_VALUEPROPERTY (BOOL, UseUtf8)
	DECLARE_HTTPCLIENT_VALUEPROPERTY (UINT, AnsiCodePage)


	STDMETHOD(ClearHeader)(BOOL * pbCleared) ;
	STDMETHOD(RemoveHeaderAt)(DWORD nIdx, BOOL * pbRemoved) ;
	STDMETHOD(RemoveHeader)(PCWSTR szName, DWORD nIdx, BOOL * pbRemoved) ;
	STDMETHOD(RemoveHeader)(const BSTR szName, DWORD nIdx, BOOL * pbRemoved) ;
	STDMETHOD(RemoveAllHeader)(PCWSTR szName, BOOL * pbRemoved) ;
	STDMETHOD(RemoveAllHeader)(const BSTR szName, BOOL * pbRemoved) ;

	STDMETHOD(AddHeader)(PCWSTR szName, PCWSTR szValue) ;
	STDMETHOD(AddHeader)(const BSTR bstrName, const BSTR bstrValue) ;
	STDMETHOD(SetHeader)(PCWSTR szName, PCWSTR szValue, DWORD nIdx) ;
	STDMETHOD(SetHeader)(const BSTR bstrName, const BSTR bstrValue, DWORD nIdx) ;

	STDMETHOD(GetHeaderNameAtLen)(DWORD nIdx, DWORD * pdwLen) ;
	STDMETHOD(GetHeaderNameAtIntoBuff)(OLECHAR * szBuff, DWORD cchBuff, DWORD nIdx) ;
	STDMETHOD(GetHeaderNameAt)(DWORD nIdx, BSTR * pbstrName) ;

	STDMETHOD(GetHeaderAtLen)(DWORD nIdx, DWORD * pdwLen) ;
	STDMETHOD(GetHeaderAtIntoBuff)(OLECHAR * szBuff, DWORD cchBuff, DWORD nIdx) ;
	STDMETHOD(GetHeaderAt)(DWORD nIdx, BSTR * pbstrHeader) ;

	STDMETHOD(GetHeaderLen)(PCWSTR szName, DWORD nIdx, DWORD * pdwLen) ;
	STDMETHOD(GetHeaderLen)(const BSTR bstrName, DWORD nIdx, DWORD * pdwLen) ;
	STDMETHOD(GetHeaderIntoBuff)(OLECHAR * szBuff, DWORD cchBuff, PCWSTR szName, DWORD nIdx) ;
	STDMETHOD(GetHeader)(PCWSTR szName, DWORD nIdx, BSTR * pbstrHeader) ;
	STDMETHOD(GetHeader)(const BSTR bstrName, DWORD nIdx, BSTR * pbstrHeader) ;

	STDMETHOD(HeaderExists)(PCWSTR szName, DWORD nIdx, BOOL * pbExist) ;
	STDMETHOD(HeaderExists)(const BSTR bstrName, DWORD nIdx, BOOL * pbExist) ;

	STDMETHOD(GetHeaderCount)(PCWSTR szName, DWORD * pdwCount) ;
	STDMETHOD(GetHeaderCount)(const BSTR bstrName, DWORD * pdwCount) ;
	STDMETHOD(GetAllHeaderCount)(DWORD * pdwCount) ;

	STDMETHOD(ClearParam)(BOOL * pbCleared) ;
	STDMETHOD(RemoveParamAt)(DWORD nIdx, BOOL * pbRemoved) ;
	STDMETHOD(RemoveParam)(PCWSTR szName, DWORD nIdx, BOOL * pbRemoved) ;
	STDMETHOD(RemoveParam)(const BSTR szName, DWORD nIdx, BOOL * pbRemoved) ;
	STDMETHOD(RemoveAllParam)(PCWSTR szName, BOOL * pbRemoved) ;
	STDMETHOD(RemoveAllParam)(const BSTR szName, BOOL * pbRemoved) ;

	STDMETHOD(AddParam)(PCWSTR szName, PCWSTR szValue, DWORD dwParamAttr) ;
	STDMETHOD(AddParam)(const BSTR bstrName, const BSTR bstrValue, DWORD dwParamAttr) ;
	STDMETHOD(SetParam)(PCWSTR szName, PCWSTR szValue, DWORD dwParamAttr, DWORD nIdx) ;
	STDMETHOD(SetParam)(const BSTR bstrName, const BSTR bstrValue, DWORD dwParamAttr, DWORD nIdx) ;

	STDMETHOD(GetParamNameAtLen)(DWORD nIdx, DWORD * pdwLen) ;
	STDMETHOD(GetParamNameAtIntoBuff)(OLECHAR * szBuff, DWORD cchBuff, DWORD nIdx) ;
	STDMETHOD(GetParamNameAt)(DWORD nIdx, BSTR * pbstrName) ;

	STDMETHOD(GetParamAtLen)(DWORD nIdx, DWORD * pdwLen) ;
	STDMETHOD(GetParamAtIntoBuff)(OLECHAR * szBuff, DWORD cchBuff, DWORD nIdx) ;
	STDMETHOD(GetParamAt)(DWORD nIdx, BSTR * pbstrParam) ;

	STDMETHOD(GetParamLen)(PCWSTR szName, DWORD nIdx, DWORD * pdwLen) ;
	STDMETHOD(GetParamLen)(const BSTR bstrName, DWORD nIdx, DWORD * pdwLen) ;
	STDMETHOD(GetParamIntoBuff)(OLECHAR * szBuff, DWORD cchBuff, PCWSTR szName, DWORD nIdx) ;
	STDMETHOD(GetParam)(PCWSTR szName, DWORD nIdx, BSTR * pbstrParam) ;
	STDMETHOD(GetParam)(const BSTR bstrName, DWORD nIdx, BSTR * pbstrParam) ;

	STDMETHOD(GetParamAttrAt)(DWORD nIdx, DWORD * pdwAttr) ;
	STDMETHOD(GetParamAttr)(PCWSTR szName, DWORD nIdx, DWORD * pdwAttr) ;
	STDMETHOD(GetParamAttr)(const BSTR bstrName, DWORD nIdx, DWORD * pdwAttr) ;

	STDMETHOD(ParamExists)(PCWSTR szName, DWORD nIdx, BOOL * pbExist) ;
	STDMETHOD(ParamExists)(const BSTR bstrName, DWORD nIdx, BOOL * pbExist) ;

	STDMETHOD(GetParamCount)(PCWSTR szName, DWORD * pdwCount) ;
	STDMETHOD(GetParamCount)(const BSTR bstrName, DWORD * pdwCount) ;
	STDMETHOD(GetAllParamCount)(DWORD * pdwCount) ;

	STDMETHOD(SetInternet)(PCWSTR szUserAgent, DWORD dwAccessType, PCWSTR szProxyName, PCWSTR szProxyBypass, DWORD dwFlags) ;
	STDMETHOD(SetInternet)(const BSTR bstrUserAgent, DWORD dwAccessType, const BSTR bstrProxyName, const BSTR bstrProxyBypass, DWORD dwFlags) ;


	DECLARE_HTTPCLIENT_STRINGPROPERTY (InternetUserAgent)
	DECLARE_HTTPCLIENT_VALUEPROPERTY (DWORD, InternetAccessType)
	DECLARE_HTTPCLIENT_STRINGPROPERTY (InternetProxyName)
	DECLARE_HTTPCLIENT_STRINGPROPERTY (InternetProxyBypass)
	DECLARE_HTTPCLIENT_VALUEPROPERTY (DWORD, InternetFlags)


	STDMETHOD(MakeGetUrlLen)(PCWSTR szUrl, DWORD * pdwLen) ;
	STDMETHOD(MakeGetUrlLen)(const BSTR bstrUrl, DWORD * pdwLen) ;
	STDMETHOD(MakeGetUrlIntoBuff)(OLECHAR * szBuff, DWORD cchBuff, PCWSTR szUrl) ;
	STDMETHOD(MakeGetUrl)(PCWSTR szUrl, BSTR * pbstrGetUrl) ;
	STDMETHOD(MakeGetUrl)(const BSTR bstrUrl, BSTR * pbstrGetUrl) ;

	STDMETHOD(SetProxyAccount)(PCWSTR szUserName, PCWSTR szPassword) ;
	STDMETHOD(SetProxyAccount)(const BSTR bstrUserName, const BSTR bstrPassword) ;

	DECLARE_HTTPCLIENT_STRINGPROPERTY_READONLY (ProxyUserName)
	DECLARE_HTTPCLIENT_STRINGPROPERTY_READONLY (ProxyPassword)

	STDMETHOD(RequestGet)(LPCWSTR szUrl, BOOL bUseCache, IHttpResponse2 ** ppIHttpResponse2) ;
	STDMETHOD(RequestGet)(const BSTR bstrUrl, BOOL bUseCache, IHttpResponse2 ** ppIHttpResponse2) ;
	STDMETHOD(RequestGetEx)(LPCWSTR szUrl, DWORD dwFlags, LPCWSTR szReferer, LPCWSTR szUsrName, LPCWSTR szUsrPwd, IHttpResponse2 ** ppIHttpResponse2) ;
	STDMETHOD(RequestGetEx)(const BSTR bstrUrl, DWORD dwFlags, const BSTR bstrReferer, const BSTR bstrUsrName, const BSTR bstrUsrPwd, IHttpResponse2 ** ppIHttpResponse2) ;

	STDMETHOD(RequestGet)(LPCWSTR szUrl, BOOL bUseCache, IDispHttpResponse2 ** ppIDispHttpResponse2) ;
	STDMETHOD(RequestGet)(const BSTR bstrUrl, BOOL bUseCache, IDispHttpResponse2 ** ppIDispHttpResponse2) ;
	STDMETHOD(RequestGetEx)(LPCWSTR szUrl, DWORD dwFlags, LPCWSTR szReferer, LPCWSTR szUsrName, LPCWSTR szUsrPwd, IDispHttpResponse2 ** ppIDispHttpResponse2) ;
	STDMETHOD(RequestGetEx)(const BSTR bstrUrl, DWORD dwFlags, const BSTR bstrReferer, const BSTR bstrUsrName, const BSTR bstrUsrPwd, IDispHttpResponse2 ** ppIDispHttpResponse2) ;


	STDMETHOD(BeginPost)(LPCWSTR szUrl, BOOL bUseCache) ;
	STDMETHOD(BeginPost)(const BSTR bstrUrl, BOOL bUseCache) ;
	STDMETHOD(BeginPostEx)(LPCWSTR szUrl, DWORD dwFlags, LPCWSTR szReferer, LPCWSTR szUsrName, LPCWSTR szUsrPwd) ;
	STDMETHOD(BeginPostEx)(const BSTR bstrUrl, DWORD dwFlags, const BSTR bstrReferer, const BSTR bstrUsrName, const BSTR bstrUsrPwd) ;

	STDMETHOD(BeginUpload)(LPCWSTR szUrl, BOOL bUseCache) ;
	STDMETHOD(BeginUpload)(const BSTR bstrUrl, BOOL bUseCache) ;
	STDMETHOD(BeginUploadEx)(LPCWSTR szUrl, DWORD dwFlags, LPCWSTR szReferer, LPCWSTR szUsrName, LPCWSTR szUsrPwd) ;
	STDMETHOD(BeginUploadEx)(const BSTR bstrUrl, DWORD dwFlags, const BSTR bstrReferer, const BSTR bstrUsrName, const BSTR bstrUsrPwd) ;

	STDMETHOD(Query)(IDispHttpPostStat2 * pIDispHttpPostStat2) ;
	STDMETHOD(Query)(IHttpPostStat2 * pIHttpPostStat2) ;

	STDMETHOD(Cancel)(BOOL * pbCanceled) ;
	STDMETHOD(Proceed)(DWORD cbDesired, IHttpResponse2 ** ppIHttpResponse2) ;
	STDMETHOD(Proceed)(DWORD cbDesired, IDispHttpResponse2 ** ppIDispHttpResponse2) ;

	STDMETHOD(RequestPost)(LPCWSTR szUrl, BOOL bUseCache, IHttpResponse2 ** ppIHttpResponse2) ;
	STDMETHOD(RequestPost)(const BSTR bstrUrl, BOOL bUseCache, IHttpResponse2 ** ppIHttpResponse2) ;
	STDMETHOD(RequestPostEx)(LPCWSTR szUrl, DWORD dwFlags, LPCWSTR szReferer, LPCWSTR szUsrName, LPCWSTR szUsrPwd, IHttpResponse2 ** ppIHttpResponse2) ;
	STDMETHOD(RequestPostEx)(const BSTR bstrUrl, DWORD dwFlags, const BSTR bstrReferer, const BSTR bstrUsrName, const BSTR bstrUsrPwd, IHttpResponse2 ** ppIHttpResponse2) ;

	STDMETHOD(RequestPost)(LPCWSTR szUrl, BOOL bUseCache, IDispHttpResponse2 ** ppIDispHttpResponse2) ;
	STDMETHOD(RequestPost)(const BSTR bstrUrl, BOOL bUseCache, IDispHttpResponse2 ** ppIDispHttpResponse2) ;
	STDMETHOD(RequestPostEx)(LPCWSTR szUrl, DWORD dwFlags, LPCWSTR szReferer, LPCWSTR szUsrName, LPCWSTR szUsrPwd, IDispHttpResponse2 ** ppIDispHttpResponse2) ;
	STDMETHOD(RequestPostEx)(const BSTR bstrUrl, DWORD dwFlags, const BSTR bstrReferer, const BSTR bstrUsrName, const BSTR bstrUsrPwd, IDispHttpResponse2 ** ppIDispHttpResponse2) ;

	STDMETHOD(RequestUpload)(LPCWSTR szUrl, BOOL bUseCache, IHttpResponse2 ** ppIHttpResponse2) ;
	STDMETHOD(RequestUpload)(const BSTR bstrUrl, BOOL bUseCache, IHttpResponse2 ** ppIHttpResponse2) ;
	STDMETHOD(RequestUploadEx)(LPCWSTR szUrl, DWORD dwFlags, LPCWSTR szReferer, LPCWSTR szUsrName, LPCWSTR szUsrPwd, IHttpResponse2 ** ppIHttpResponse2) ;
	STDMETHOD(RequestUploadEx)(const BSTR bstrUrl, DWORD dwFlags, const BSTR bstrReferer, const BSTR bstrUsrName, const BSTR bstrUsrPwd, IHttpResponse2 ** ppIHttpResponse2) ;

	STDMETHOD(RequestUpload)(LPCWSTR szUrl, BOOL bUseCache, IDispHttpResponse2 ** ppIDispHttpResponse2) ;
	STDMETHOD(RequestUpload)(const BSTR bstrUrl, BOOL bUseCache, IDispHttpResponse2 ** ppIDispHttpResponse2) ;
	STDMETHOD(RequestUploadEx)(LPCWSTR szUrl, DWORD dwFlags, LPCWSTR szReferer, LPCWSTR szUsrName, LPCWSTR szUsrPwd, IDispHttpResponse2 ** ppIDispHttpResponse2) ;
	STDMETHOD(RequestUploadEx)(const BSTR bstrUrl, DWORD dwFlags, const BSTR bstrReferer, const BSTR bstrUsrName, const BSTR bstrUsrPwd, IDispHttpResponse2 ** ppIDispHttpResponse2) ;


private:
	Ryeol::CHttpClientW			m_objHttpClient ;

	HRESULT _CheckActivePost (PCWSTR szTargetName) throw () ;
	HRESULT _CopyStringToBuff (PWSTR szBuff, DWORD cchBuff, PCWSTR szSrc) throw () ;
	HRESULT _DupStringAsSysStr (BSTR * pbstrDest, PCWSTR szSrc, BOOL bSetNull) throw () ;

	template<typename QueriedInterface>
	HRESULT _GetIHttpResponse2 (Ryeol::CHttpResponseW * pobjHttpRes, QueriedInterface ** ppQueriedInterface) throw () ;
};

OBJECT_ENTRY_AUTO(__uuidof(HttpClient2), CHttpClient)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Korea (Republic of) Korea (Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions