Click here to Skip to main content
13,632,371 members
Rate this:
 
Please Sign up or sign in to vote.
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++

#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 if( isset($_POST['teststr']) ) echo $_POST['teststr']; ?>


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

Thank you.
Posted 8-Sep-14 11:08am
Comments
bling 2-Oct-14 13:11pm
   
For completeness, call InternetCloseHandle on the hFile handle.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Add this ...

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


and modify your call to

bool res = HttpSendRequest(hFile, headers, sizeof headers, (char*)postData, dataLen);
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.180712.1 | Last Updated 2 Oct 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100