Simple httpsendrequest post data to index page

Sep 8, 2014 at 11:59am
I"m trying to post some string to a localhost index.php page using httpsendrequest but it's not working. The application runs correctly, every function returns correctly but the data posted is not shown on the index.php page. This is the code that I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    #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,
    		"Content-type: application/x-www-form-urlencoded",
    		-1,  // this calculates the strlen
    		(char*)postData,
    		dataLen
    	);
    	if( !res )
    	{
    		printf("Unable to send request. Error %ld\n", GetLastError());
    		exit(1);
    	}
    	printf("Request sent\n");
    	
    	return 0;
    }


and the php file

1
2
3
4
5
6
7
8
9
10
    <?php
    
    	$output = "Heya";
    	if( isset($_POST['teststr']) )
    	{
            $output .= $_POST['teststr'];
            echo $output;
    	}
    
    ?>


but it doesn't echo the output even though the application is ran or still running.
Last edited on Sep 10, 2014 at 11:35am
Sep 8, 2014 at 3:16pm
There is an example from Microsoft itself here:
http://support.microsoft.com/kb/165298

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static TCHAR hdrs[] =
      _T("Content-Type: application/x-www-form-urlencoded");
   static TCHAR frmdata[] =
      _T("name=John+Doe&userid=hithere&other=P%26Q");
  static LPSTR accept[2]={"*/*", NULL};

   // for clarity, error-checking has been removed
   HINTERNET hSession = InternetOpen("MyAgent",
      INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
   HINTERNET hConnect = InternetConnect(hSession, _T("ServerNameHere"),
      INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
   HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",
      _T("FormActionHere"), NULL, NULL, accept, 0, 1);
   HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
   // close any valid internet-handles 
Last edited on Sep 8, 2014 at 3:17pm
Sep 8, 2014 at 4:22pm
I already tried that, and it doesn't work..
where's the PHP code? I used that to make the C code. @modoran
Topic archived. No new replies allowed.