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

📄 requestobject.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
字号:
/********************************************************************/
/*																	*/
/*  RequestObject.cpp												*/
/*																	*/
/*  Implementation of the CRequestObject class.						*/
/*																	*/
/*  Programmed by Pablo van der Meer								*/
/*	This code is stolen from: http://www.pablovandermeer.nl			*/
/*																	*/
/*  Last updated: July 4, 2003										*/
/*																	*/
/********************************************************************/

#include "stdafx.h"
#include "RequestObject.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


IMPLEMENT_DYNCREATE(CRequestObject, CCmdTarget)


/********************************************************************/
/*																	*/
/* Function name : CRequestObject::CRequestObject					*/
/* Description   : Constructor										*/
/*																	*/
/********************************************************************/
CRequestObject::CRequestObject()
{
	EnableAutomation();

	try 
	{
		// create instance of "Scripting.Dictionary" using smart pointer for QueryString collection
		HRESULT hResult = m_pQueryStringCollection.CreateInstance(L"Scripting.Dictionary");
		if (SUCCEEDED(hResult))
		{
			TRACE0("QueryString collection succesfully loaded!\n");
		}
		// create instance of "Scripting.Dictionary" using smart pointer for Form collection
		hResult = m_pFormCollection.CreateInstance(L"Scripting.Dictionary");
		if (SUCCEEDED(hResult))
		{
			TRACE0("Form collection succesfully loaded!\n");
		}
		// create instance of "Scripting.Dictionary" using smart pointer for ServerVariables collection
		hResult = m_pServerVariablesCollection.CreateInstance(L"Scripting.Dictionary");
		if (SUCCEEDED(hResult))
		{
			TRACE0("ServerVariables collection succesfully loaded!\n");
		}
	}
	// Catch all MFC exceptions, including COleExceptions.
	// OS exceptions will not be caught.
	catch (CException *e) 
	{
		CString strError;

		strError.Format("%s(%d): OLE Exception caught: SCODE = %x", 
			__FILE__, __LINE__, COleException::Process(e));

		TRACE1("%s\n", strError);

		char szError[128];

		e->GetErrorMessage(szError, sizeof(szError));
		
		TRACE1("%s\n", szError);

		e->Delete();
	}
	catch (_com_error e)
	{
	}
}


/********************************************************************/
/*																	*/
/* Function name : CRequestObject::~CRequestObject					*/
/* Description   : Destructor										*/
/*																	*/
/********************************************************************/
CRequestObject::~CRequestObject()
{
}


BEGIN_MESSAGE_MAP(CRequestObject, CCmdTarget)
	//{{AFX_MSG_MAP(CRequestObject)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


BEGIN_DISPATCH_MAP(CRequestObject, CCmdTarget)
	//{{AFX_DISPATCH_MAP(CRequestObject)
	DISP_FUNCTION(CRequestObject, "QueryString", QueryString, VT_DISPATCH, VTS_NONE)
	DISP_FUNCTION(CRequestObject, "Form", Form, VT_DISPATCH, VTS_NONE)
	DISP_FUNCTION(CRequestObject, "ServerVariables", ServerVariables, VT_DISPATCH, VTS_NONE)
	DISP_FUNCTION(CRequestObject, "Cookies", Cookies, VT_DISPATCH, VTS_NONE)
	DISP_PROPERTY_PARAM(CRequestObject, "Item", GetItem, SetNotSupported, VT_BSTR, VTS_BSTR)
	DISP_DEFVALUE(CRequestObject, "Item")
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IRequestObject to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {8391C3A1-145D-424B-891E-51A5836FA1B4}
static const IID IID_IRequestObject =
{ 0x8391c3a1, 0x145d, 0x424b, { 0x89, 0x1e, 0x51, 0xa5, 0x83, 0x6f, 0xa1, 0xb4 } };

BEGIN_INTERFACE_MAP(CRequestObject, CCmdTarget)
	INTERFACE_PART(CRequestObject, IID_IRequestObject, Dispatch)
END_INTERFACE_MAP()


/********************************************************************/
/*																	*/
/* Function name : QueryString										*/
/* Description   : Return QueryStringCollection IDispatch pointer 	*/
/*																	*/
/********************************************************************/
LPDISPATCH CRequestObject::QueryString() 
{
	m_pQueryStringCollection->AddRef();
	return m_pQueryStringCollection;
}


/********************************************************************/
/*																	*/
/* Function name : Form												*/
/* Description   : Return Form IDispatch pointer 					*/
/*																	*/
/********************************************************************/
LPDISPATCH CRequestObject::Form() 
{
	m_pFormCollection->AddRef();
	return m_pFormCollection;
}


/********************************************************************/
/*																	*/
/* Function name : ServerVariables									*/
/* Description   : Return ServerVariables IDispatch pointer 		*/
/*																	*/
/********************************************************************/
LPDISPATCH CRequestObject::ServerVariables() 
{
	m_pServerVariablesCollection->AddRef();
	return m_pServerVariablesCollection;
}


/********************************************************************/
/*																	*/
/* Function name : Cookies											*/
/* Description   : Return Cookies IDispatch pointer 				*/
/*																	*/
/********************************************************************/
LPDISPATCH CRequestObject::Cookies() 
{
	return m_CookieCollection.GetIDispatch(TRUE);
}


/********************************************************************/
/*																	*/
/* Function name : AddQueryStringKey								*/
/* Description   : Add an entry to the QueryString collection.		*/
/*																	*/
/********************************************************************/
void CRequestObject::AddQueryStringKey(LPCTSTR lpszKey, LPCTSTR lpszValue)
{
	CString strValue = URLDecode(lpszValue);

	COleVariant varKey(lpszKey);
	COleVariant varValue(strValue);

	HRESULT hResult = m_pQueryStringCollection->Add(varKey, varValue);
	if (SUCCEEDED(hResult))
	{
		TRACE0("Key succesfully added to QueryString collection\n");
	}
}


/********************************************************************/
/*																	*/
/* Function name : AddFormKey										*/
/* Description   : Add an entry to the Form collection.				*/
/*																	*/
/********************************************************************/
void CRequestObject::AddFormKey(LPCTSTR lpszKey, LPCTSTR lpszValue)
{
	CString strValue = URLDecode(lpszValue);

	COleVariant varKey(lpszKey);
	COleVariant varValue(strValue);

	HRESULT hResult = m_pFormCollection->Add(varKey, varValue);
	if (SUCCEEDED(hResult))
	{
		TRACE0("Key succesfully added to Form collection\n");
	}
}


/********************************************************************/
/*																	*/
/* Function name : AddServerVariablesKey							*/
/* Description   : Add an entry to the ServerVariables collection.	*/
/*																	*/
/********************************************************************/
void CRequestObject::AddServerVariablesKey(LPCTSTR lpszKey, LPCTSTR lpszValue)
{
	COleVariant varKey(lpszKey);
	COleVariant varValue(lpszValue);

	HRESULT hResult = m_pServerVariablesCollection->Add(varKey, varValue);
	if (SUCCEEDED(hResult))
	{
		TRACE0("Key succesfully added to ServerVariables collection\n");
	}
}


/********************************************************************/
/*																	*/
/* Function name : AddCookiesKey									*/
/* Description   : Add an entry to the Cookies collection.			*/
/*																	*/
/********************************************************************/
void CRequestObject::AddCookiesKey(LPCTSTR lpszName, LPCTSTR lpszValue)
{
	CString strValue = URLDecode(lpszValue);

	if (m_CookieCollection.Add(lpszName, strValue))
	{
		TRACE0("Key succesfully added to Cookie collection\n");
	}
}


/************************************************************************/
/*																		*/
/* Function name : GetItem												*/
/* Description   : Retrieves the value for the specified variable		*/
/*																		*/
/************************************************************************/
BSTR CRequestObject::GetItem(LPCTSTR lpszVar) 
{
	CString strResult = "";

	COleVariant varValue;
	COleVariant varKey(lpszVar);
	
	// check if key exists in Form collection
	varValue = m_pFormCollection->GetItem(varKey);
	if (varValue.vt == VT_BSTR)
	{
		strResult = CString(varValue.bstrVal);
		return strResult.AllocSysString();
	}

	// check if key exists in QueryString collection
	varValue = m_pQueryStringCollection->GetItem(varKey);
	if (varValue.vt == VT_BSTR)
	{
		strResult = CString(varValue.bstrVal);
		return strResult.AllocSysString();
	}

	// check if key exists in Cookie collection
	varValue = m_CookieCollection.GetItem(varKey);
	if (varValue.vt == VT_DISPATCH)
	{
		// convert to BSTR
		varValue.ChangeType(VT_BSTR);

		strResult = CString(varValue.bstrVal);
		if (!strResult.IsEmpty())
			return strResult.AllocSysString();
	}

	// check if key exists in ServerVariables collection
	varValue = m_pServerVariablesCollection->GetItem(varKey);
	if (varValue.vt == VT_BSTR)
	{
		strResult = CString(varValue.bstrVal);
		return strResult.AllocSysString();
	}
	// key not found
	return strResult.AllocSysString();
}

⌨️ 快捷键说明

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