Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been trying to post some code using httpsendrequest and I've looked all over the internet to a point that I started getting the exact same results i've been going through. I finally came up with some code that posts a very simple string to a local server. The app runs perfectly but the output isn't shown on the server file that's receiving it.

C++

C++
#define _WIN32_WINNT 0x600

#include <stdio.h>
#include <wininet.h>

#define BUFLEN 200

static const char *acceptTypes[] = {"application/x-www-form-urlencoded", NULL};
static const char *postData = "teststr=Hello+world&testval=42";

int main()
{
	HINTERNET hSession, hConnect, hFile;
	
	if( ( hSession = InternetOpen(
		"myapp",
		INTERNET_OPEN_TYPE_PRECONFIG,
		NULL,
		NULL,
		0
	) ) == NULL )
	{
		printf("Couldn't start session. Error %ld\n", GetLastError());
		exit(1);
	}
	printf("Session started\n");
	
	if( ( hConnect = InternetConnect(
		hSession,
		"localhost",
		INTERNET_DEFAULT_HTTP_PORT,
		NULL,
		NULL,
		INTERNET_SERVICE_HTTP,
		0,
		0
	) ) == NULL )
	{
		printf("Unable to connect to server. Error %ld\n", GetLastError());
		exit(1);
	}
	printf("Connected to server\n");
	
	if( ( hFile = HttpOpenRequest(
		hConnect,
		"POST",
		"/test/index.php",
		NULL,
		NULL,
		acceptTypes,
		INTERNET_FLAG_RELOAD,
		0
	) ) == NULL )
	{
		printf("Unable to open request. Error %ld\n", GetLastError());
		exit(1);
	}
	printf("Opening request..Opened\n");
	
	unsigned long dataLen = strlen(postData)+1;
	bool res = HttpSendRequest(
		hFile,
		NULL,
		0,
		(char*)postData,
		dataLen
	);
	if( !res )
	{
		printf("Unable to send request. Error %ld\n", GetLastError());
		exit(1);
	}
	printf("Request sent\n");
	
	return 0;
}


PHP
PHP
<?php if( isset($_POST['teststr']) ) echo $_POST['teststr']; ?>


What am I doing wrong?
In addition, a simpler example would do me good.

Thank you.
Posted
Comments
[no name] 2-Oct-14 13:11pm    
For completeness, call InternetCloseHandle on the hFile handle.

1 solution

Add this ...

C++
char headers[] = "Content-Type: application/x-www-form-urlencoded\r\n";


and modify your call to

C++
bool res = HttpSendRequest(hFile, headers, sizeof headers, (char*)postData, dataLen);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900