Posting HTTP forms programmatically

Started by
2 comments, last by Wan 18 years, 10 months ago
I'm having some trouble getting my form-posting code to work. The server responds as if I haven't posted anything. Form address: http://www.xwis.net/xwi/?a=leave My code:
void main()
{
	printf("Creating internet session...\n");
	HINTERNET internetSession = InternetOpen("XWIS_TMC",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL);
	if (internetSession)
	{
		printf("Connecting...\n");
		HINTERNET internetConn = InternetConnect(internetSession,"www.xwis.net",INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,NULL,NULL);
		if (internetConn)
		{
			printf("Creating HTTP request...\n");
			LPCTSTR acceptTypes[2] = { "*/*", NULL };
			HINTERNET httpReq = HttpOpenRequest(internetConn,"POST","xwi/?a=leave","HTTP/4.01",NULL,acceptTypes,INTERNET_FLAG_RELOAD|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_PRAGMA_NOCACHE,NULL);
			if (httpReq)
			{
				printf("Adding request headers...\n");
				const char * headers = "Content-Type: application/x-www-form-urlencoded";
				printf("Sending HTTP request...\n");
				const char * postData = "name=fonger&pass=blah";
				if (HttpSendRequest(httpReq,headers,strlen(headers),(LPVOID)postData,strlen(postData) + 1))
				{
					printf("Awaiting response...\n");

					char buf[1024];
					DWORD numRead = 1;
					while (InternetReadFile(httpReq,buf,1023,&numRead))
					{
						if (numRead == 0)
							break;
						buf[numRead] = 0;
						cout << buf;
					}
				} else
				{
					printf("\tERROR: %u\n",GetLastError());
				}
				InternetCloseHandle(httpReq);
			}
			InternetCloseHandle(internetConn);
		}
		InternetCloseHandle(internetSession);
	}
	printf("Complete.\n");
	system("pause");
}
Is there anything obvious I'm missing? This is for a clan manager for Red Alert 2; no abuse intended :p
Advertisement
I think this might help you get an idea of how its done; I think its a bit differen't then the way your doing it though.
- GDKnight
Your HTTP version string (HTTP/4.01) is bogus. The current standard version of HTTP is 1.1, and the vast bulk of servers out there expect 1.1. It's possible that the server is seeing your bogus version string and rejecting the input because it doesn't support that version (naturally). In any case it is very bad etiquette to send a bogus protocol version, so I'd suggest changing that to 1.1.

Aside from that, the best I can suggest is to check all the parameters (URL, headers, form field names) and make sure everything is valid. Possibly point the code at a dummy script of your own someplace to make sure it works; if it works on a dummy script but not the live version, it's possible there's some kind of validation check that you're not accounting for.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Quote:Original post by ApochPiQ
...it's possible there's some kind of validation check that you're not accounting for.

And if that is the case, maybe try to include a user agent, referer etc. in your headers so it looks the request came from a "normal" browser.

This topic is closed to new replies.

Advertisement