PostMon.exe Demonstrates Using URL Moniker to POST DataID: Q165800
|
Depending on the quantity of data to be transmitted in a request to an HTTP server, it may be desirable, or even necessary, to use the POST method as opposed to the GET method, which is limited to sending approximately 2K of data to the server. This article explains the requirements for using URL Monikers to POST data to an HTTP server. PostMon.exe is a sample a dialog- based application that demonstrates the modifications necessary to perform a POST.
The following file is available for download from the Microsoft Software
Library:
~ PostMon.exe
For more information about downloading files from the Microsoft Software
Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online Services
STDMETHODIMP
CBindStatusCallback::GetBindInfo(DWORD* pgrfBINDF,
BINDINFO* pbindInfo)
{
if (m_fRedirect && BINDVERB_POST == m_dwAction)
{
// You are being redirected by the server.
// The method is changed to GET here so no data is posted below
SetStatusText(_T("Switching method to GET"));
m_dwAction = BINDVERB_GET;
}
pgrfBINDF = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE
| BINDF_PULLDATA;
pgrfBINDF |= BINDF_GETNEWESTVERSION
| BINDF_NOWRITECACHE;
// Set up the BINDINFO data structure
pbindInfo->cbSize = sizeof(BINDINFO);
pbindInfo->dwBindVerb = m_dwAction; // here's where the action is
pbindInfo->szExtraInfo = NULL;
// Initialize the STGMEDIUM.
memset(&pbindInfo->stgmedData, 0, sizeof(STGMEDIUM));
pbindInfo->grfBindInfoF = 0;
pbindInfo->szCustomVerb = NULL;
// set up action-specific members
switch(m_dwAction)
{
case BINDVERB_POST:
// only POST data when the method is POST
if (m_hDataToPost)
{
// Fill the STGMEDIUM with the data to post
// this is the only medium urlmon supports right now
pbindInfo->stgmedData.tymed = TYMED_HGLOBAL;
pbindInfo->stgmedData.hGlobal = m_hDataToPost;
pbindInfo->stgmedData.pUnkForRelease =
(LPUNKNOWN)(LPBINDSTATUSCALLBACK)this; // maintain control
AddRef(); // It will be freed on final release
// the following must be exact! GlobalSize() rounds up.
pbindInfo->cbstgmedData = m_cbDataToPost;
}
break;
case BINDVERB_GET:
break;
default:
return E_FAIL;
}
When automatically redirecting a POST request after receiving a 301 status code, some existing HTTP/1.0 user agents will erroneously change it into a GET request.For compatibility with Netscape Navigator, the URL moniker performs such an action.
STDMETHODIMP
CBindStatusCallback::BeginningTransaction(LPCWSTR szURL,
LPCWSTR szHeaders,
DWORD dwReserved,
LPWSTR __RPC_FAR *pszAdditionalHeaders)
{
// Here's our opportunity to add headers
if (!pszAdditionalHeaders)
{
return E_POINTER;
}
*pszAdditionalHeaders = NULL;
// This header is required when performing a POST operation
if (BINDVERB_POST == m_dwAction && m_hDataToPost)
{
const WCHAR c_wszHeaders[] =
L"Content-Type: application/x-www-form-urlencoded\r\n";
LPWSTR wszAdditionalHeaders =
(LPWSTR)CoTaskMemAlloc(
(wcslen(c_wszHeaders)+1) *sizeof(WCHAR));
if (!wszAdditionalHeaders)
{
return E_OUTOFMEMORY;
}
wcscpy(wszAdditionalHeaders, c_wszHeaders);
*pszAdditionalHeaders = wszAdditionalHeaders;
}
return NOERROR;
}
STDMETHODIMP CBindStatusCallback::OnResponse(DWORD dwResponseCode,
LPCWSTR szResponseHeaders,
LPCWSTR szRequestHeaders,
LPWSTR __RPC_FAR *pszAdditionalRequestHeaders)
{
if (!pszAdditionalRequestHeaders)
{
return E_POINTER;
}
*pszAdditionalRequestHeaders = NULL;
return NOERROR;
}
Berners-Lee, T. RFC 1945, "The Hypertext Transfer Protocol -- HTTP/1.0"
Fielding, R. RFC 2068, "Hypertext Transfer Protocol -- HTTP/1.1"
Asynchronous Moniker Specification included with the ActiveX SDK
Additional query words:
Keywords : kbfile kbsample kbIE300 kbIE500 AXSDKUrlMon
Version : WINDOWS:3.0,5.0
Platform : WINDOWS
Issue type : kbinfo
Last Reviewed: May 6, 1999