Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying post some data to one php server that accept data by "POST" method.
I am using below standard code that I found in MSDN.
My server address is: www.arad.com//up/up.php
Data want Post is: "/MT4i TradeCopy Sender MT4iTradeCopy 2013-10-11"

And my code is:
C++
#include "StdAfx.h"

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#include "winhttp.h"
#pragma comment (lib,"Winhttp.lib")
int main(int argc, char *argv[])
{
	LPCWSTR server=L"www.arad.com";
	LPCWSTR ext=L"/up/up.php";
	LPCWSTR data=L"/MT4i TradeCopy Sender MT4iTradeCopy 2013-10-11";

	BOOL  bResults = FALSE;
	HINTERNET hSession = NULL,
		hConnect = NULL,
		hRequest = NULL;

	// Use WinHttpOpen to obtain a session handle.
	hSession = WinHttpOpen(  L"A WinHTTP Example Program/1.0", 
		WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
		WINHTTP_NO_PROXY_NAME, 
		WINHTTP_NO_PROXY_BYPASS, 0);

	// Specify an HTTP server.
	if (hSession)
		hConnect = WinHttpConnect( hSession, server,
		INTERNET_DEFAULT_HTTP_PORT, 0);

	// Create an HTTP Request handle.
	if (hConnect)
		hRequest = WinHttpOpenRequest( hConnect, L"POST", 
		0, 
		ext, WINHTTP_NO_REFERER, 
		WINHTTP_DEFAULT_ACCEPT_TYPES,
		0);

	// Send a Request.
	if (hRequest) 
		bResults = WinHttpSendRequest( hRequest, 
		//WINHTTP_NO_ADDITIONAL_HEADERS,
		WINHTTP_NO_ADDITIONAL_HEADERS,
		0, (LPVOID)data, wcslen(data), 
		wcslen(data), 0);

	// PLACE ADDITIONAL CODE HERE.

	// Report any errors.
	if (!bResults)
		printf( "Error %d has occurred.\n", GetLastError());

	// Close any open handles.
	if (hRequest) WinHttpCloseHandle(hRequest);
	if (hConnect) WinHttpCloseHandle(hConnect);
	if (hSession) WinHttpCloseHandle(hSession);


	return 0;
}


But hen try POST, it send nothing to server. How can I fix that?
Another question:
What is max length for data that can Post to server?
Regards,
Posted
Updated 13-Oct-13 0:16am
v2
Comments
pasztorpisti 13-Oct-13 9:15am    
One thing that is bug for sure is that the data size must be specified in bytes and not in number of wchar_t characters so you have to use wcslen(data)*sizeof(*data).
Use wireshark and its "follow tcp stream" function to check out what has actually been sent over the wire.
Mohibur Rashid 16-Oct-13 19:25pm    
do two steps.
1. Convert your data to UTF8 cause php do not operate with UNICODE
2. Convert your data to proper URI. / would be %2F , space would be + and so on

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