Link to home
Start Free TrialLog in
Avatar of Tim Titus
Tim TitusFlag for United States of America

asked on

WInHTTP posts not posting data as expected

Here's a sample web page form for doing an HTTP POST: http://www.pathsolutions.com/testform.asp

When the form is filled out and submitted, it calls testpost.asp.  Testpost.asp has:
<% = Request.Form %>

I have attached a C program that uses WinHTTP to perform an HTTP POST similar to the web page.

Why does it not work similarly to the web page post?
/**********************************************************/
/* PostStats                                              */
/* A program to post using WinHTTP functions              */
/* Compile: cl PostStats.cpp /link winhttp.lib user32.lib */
/**********************************************************/
#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
 
/*****************/
/* MAIN FUNCTION */
/*****************/
void main(int nArgc, char *pArgv[] )
{
	// Internet query values
	HINTERNET	hSession=NULL;
	HINTERNET	hConnect=NULL;
	HINTERNET	hRequest=NULL;
	BOOL		bResults=FALSE;
	DWORD		dwSize = sizeof(DWORD);
	LPSTR		pszOutBuffer=NULL;
	DWORD		dwDownloaded=0;
	BOOL		DataRead=FALSE;
	LPSTR		pszData;
	DWORD		dwBytesWritten = 0;
	LPVOID		lpOutBuffer = NULL;
	WCHAR		SSHostNameW[1000];
	WCHAR		SSURIStringW[1000];
	WCHAR		Header[1000];
 
	// Create post data
	pszData = (char *)malloc(100);
	sprintf(pszData, "name=aaaaaaaaa&Address=bbbbbbbbb");
 
	// Check for arguments
	if (nArgc != 3)	{ printf("poststats webserver page\n"); return; }
 
	// Convert parameters to wide character strings
	mbstowcs(SSHostNameW, pArgv[1], 1000);
	mbstowcs(SSURIStringW, pArgv[2], 1000);
 
	// Use WinHttpOpen to obtain a session handle.
	printf("  WinHttpOpen ");
	hSession = WinHttpOpen(	L"PostStats/1.0",
							WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
							WINHTTP_NO_PROXY_NAME,
							WINHTTP_NO_PROXY_BYPASS, 0);
 
	// Specify an HTTP server.
	if (hSession)
	{
		printf("succeeded\n  WinHttpConnect ");
		hConnect = WinHttpConnect(	hSession,
									SSHostNameW,
									INTERNET_DEFAULT_HTTP_PORT,
									0);
	}
 
	// Create an HTTP Request handle.
	if (hConnect)
	{
		printf("succeeded\n  WinHttpOpenRequest ");
		hRequest = WinHttpOpenRequest(	hConnect,
										L"PUT",
										SSURIStringW,
										NULL, WINHTTP_NO_REFERER,
										WINHTTP_DEFAULT_ACCEPT_TYPES,
										0);
	}
 
	// Send a Request.
	if (hRequest)
	{
		printf("succeeded\n  WinHttpSendRequest ");
		bResults = WinHttpSendRequest(	hRequest,
										L"Content-Type: application/x-www-form-urlencoded\r\nAccept: */*\r\n",
										-1L,
										WINHTTP_NO_REQUEST_DATA,
										0,
										strlen(pszData),
										0);
	}
 
	// Write data to the server.
	if (bResults)
	{
		printf("succeeded\n  WinHttpWriteData ");
		bResults = WinHttpWriteData(	hRequest,
										pszData,
										strlen(pszData),
										&dwBytesWritten);
	}
 
	// End the request.
	if (bResults)
	{
		printf("succeeded\n  %u bytes written\n  WinHttpReceiveResponse ", dwBytesWritten);
		bResults = WinHttpReceiveResponse( hRequest, NULL);
	}
 
	// First, use WinHttpQueryHeaders to obtain the size of the buffer.
	if (bResults)
	{
		printf("succeeded\n  WinHttpQueryHeaders(size) ");
		WinHttpQueryHeaders(	hRequest,
								WINHTTP_QUERY_RAW_HEADERS_CRLF,
								WINHTTP_HEADER_NAME_BY_INDEX,
								NULL,
								&dwSize,
								WINHTTP_NO_HEADER_INDEX);
 
		// Allocate memory for the buffer.
		if( GetLastError( ) == ERROR_INSUFFICIENT_BUFFER )
		{
			printf("succeeded\n  WinHttpQueryHeaders ");
			lpOutBuffer = new WCHAR[dwSize/sizeof(WCHAR)];
 
			// Now, use WinHttpQueryHeaders to retrieve the header.
			bResults = WinHttpQueryHeaders(	hRequest,
											WINHTTP_QUERY_RAW_HEADERS_CRLF,
											WINHTTP_HEADER_NAME_BY_INDEX,
											lpOutBuffer,
											&dwSize,
											WINHTTP_NO_HEADER_INDEX);
		}
	}
 
	// Print the header contents.
	if (bResults)
	{
		printf("succeeded\n%S\n  Headers ",lpOutBuffer);
	}
 
	// Free the allocated memory.
	delete [] lpOutBuffer;
 
	// Fetch page body
	if (bResults)
	{
		printf("succeeded\n");
		do
		{
			// Verify available data.
			dwSize = 0;
			if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
			{
		        printf("  WinHttpQueryDataAvailable failed: Error %d has occurred.\n",GetLastError());
			}
 
			// Allocate space for the buffer.
			pszOutBuffer = new char[dwSize+1];
			if (!pszOutBuffer)
			{
				printf("failed.  Out of memory\n");
				dwSize=0;
			}
			else
			{
				// Read the Data.
				ZeroMemory(pszOutBuffer, dwSize+1);
 
				if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
					printf( "  WinHttpReadData failed: Error %u has occurred.\n", GetLastError());
				else
					printf("%s\n", pszOutBuffer);
 
				// Free the memory allocated to the buffer.
				delete [] pszOutBuffer;
			}
 
		} while (dwSize>0);
		printf("  WinHttpQueryDataAvailable ");
	}
 
    // Report any errors.
    if (bResults)
    {
		printf("succeeded\n");
	}
	else
    {
        printf(" failed: Error %d has occurred.\n",GetLastError());
	}
 
    // Close any open handles.
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);
 
	free(pszData);
	return;
}

Open in new window

Avatar of jkr
jkr
Flag of Germany image

May I ask what is going wrong?
Avatar of Tim Titus

ASKER

Instead of receiving the same page back, I receive a HTTP/1.1 403 Forbidden error.  Since this should simulate the same POST as the browser, I shouldn't need to change security levels on the server.  Here's the full results of the program:

  WinHttpOpen succeeded
  WinHttpConnect succeeded
  WinHttpOpenRequest succeeded
  WinHttpSendRequest succeeded
  WinHttpWriteData succeeded
  32 bytes written
  WinHttpReceiveResponse succeeded
  WinHttpQueryHeaders(size) succeeded
  WinHttpQueryHeaders succeeded
HTTP/1.1 403 Forbidden
Content-Length: 1758
Content-Type: text/html
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Fri, 07 Mar 2008 16:28:46 GMT


  Headers succeeded
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/st
rict.dtd">
<HTML><HEAD><TITLE>The page cannot be displayed</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">
<STYLE type="text/css">
  BODY { font: 8pt/12pt verdana }
  H1 { font: 13pt/15pt verdana }
  H2 { font: 8pt/12pt verdana }
  A:link { color: red }
  A:visited { color: maroon }
</STYLE>
</HEAD><BODY><TABLE width=500 border=0 cellspacing=10><TR><TD>

<h1>The page cannot be displayed</h1>
You have attempted to execute a CGI, ISAPI, or other executable program from a d
irectory that does not allow programs to be executed.
<hr>
<p>Please try the following:</p>
<ul>
<li>Contact the Web site administrator if you believe this directory should allo
w execute access.</li>
</ul>
<h2>HTTP Error 403.1 - Forbidden: Execute access
is denied.<br>Internet Information Services (IIS)</h2>
<hr>
<p>Technical Information (for support personnel)</p>
<ul>
<li>Go to <a href="http://go.microsoft.com/fwlink/?linkid=8180">Microsoft Produc
t Support Services</a> and perform
 a title search for the words <b>HTTP</b> and <b>403</b>.</li>
<li>Open <b>IIS Help</b>, which is accessible in IIS Manager (inetmgr),
 and search for topics titled <b>Configuring ISAPI Extensions</b>, <b>Configurin
g CGI Applications</b>, <b>Securing Your Site with Web Site Permissions</b>, and
 <b>About Custom Error Messages</b>.</li>
<li>In the IIS Software Development Kit (SDK) or at the <a href="http://go.micro
soft.com/fwlink/?LinkId=8181">MSDN Online Library</a>, search for topics titled
<b>Developing ISAPI Extensions</b>, <b>ISAPI and CGI</b>, and <b>Debugging ISAPI
 Extensions and Filters</b>.</li>
</ul>

</TD></TR></TABLE></BODY></HTML>


  WinHttpQueryDataAvailable succeeded

How do I get WinHTTP POST to simulate the same thing that the testform.asp POST does?
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very good!  I missed that one!