Jump to content
  • Advertisement
Sign in to follow this  
Tim Cowley

Posting HTTP forms programmatically

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!