Error 400 Bad Response

I finally got my POST request to work.
BUT, apparently the header format is wrong.
I get error code 400 bad response, ("server could not understand")
So I ONLY need to proper header format, the code itself is fine.
Please copy-paste it and try it out yourself.

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
void HttpPost()
{
//check if internet works
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
		cout << "WSAStartup failed.\n";
		return;
	}
//connect to website
	SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	struct hostent *host;
	host = gethostbyname("www.cplusplus.com");
	SOCKADDR_IN SockAddr;
	SockAddr.sin_port = htons(80);
	SockAddr.sin_family = AF_INET;
	SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
	cout << "Connecting...\n";
	if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
		cout << "Could not connect";
		return;
	}
	cout << "Connected.\n";
//this is the header, aka the data sent to the server. HERE is the problem!
	const char* buf = "POST /login.do HTTP/1.1\n Content-Type: application/x-www-form-urlencoded\n Host: www.cplusplus.com "
		" \n Content-Length: 120 \n w=login&y=1&to=www.cplusplus.com&l=myusername&p=mypassword \r\n\r\n";
//length of the sent header
	cout << "Bytes sent: " << send(Socket, buf, strlen(buf), 0) << endl;
	cout << "Printing outputs..." << endl;
// prints out HTML response
//counter, just for info, to check how many "streams" it takes to get the response
	int count = 0; 
//variable that holds the actual response
	char buffer[10000];
	int nDataLength;
		while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
			count++;
		int i = 0;
		while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
			cout << buffer[i];
			i += 1;
		}
	}
	
//finish things, else you have a process that never ended, probably a memory leak or something
	closesocket(Socket);
	WSACleanup();
	cout << "done. Attempts: " << count << endl;
}

EDIT:
added comments
Last edited on
Can you please explain what each line does.
sure, right now im busy, but about 2-3 hours from now ill add comments
I think you should be using \r\n line breaks in the header too and I'm not sure the header lines are allowed to start with whitespace characters.

The actual POST data is not part of the header so you should place it after the double line breaks \r\n\r\n (Double line breaks marks the end of a HTTP header).
I already gave a working example in your previous post, but feel free to reinvent the wheel.
That is using BOOST, since its a library, I prefer not to use it.
I might give it a try, but chances are high I cannot get it to work.
Last edited on
@peter87 I think you are right, I found some sources that indicate my header is wrong.
Also, how do I directly reply to a comment?
Im looking for an easier method without using sockets.
I.e. WinInet,


That is using BOOST, since its a library, I prefer not to use it.


I found some sources that indicate my header is wrong.
All your end of lines should be \r\n, rather than \n. So you need to fix all of those. Remember the blank line sequence is \r\n\r\n. I haven't checked if you've missed one of those, but I think it's ok.
user123 wrote:
Also, how do I directly reply to a comment?

Normally you quote the person that you are replying to and put your text after it.



[quote=name of quoted user]quoted text[/quote]

your reply
Last edited on
Ive installed boost and it semi-works, but for the stream class I have to do something else, all other partws of the library only need #include, but this one needs something else so it gives linker error:
error LNK1104: cannot open file 'libboost_system-vc120-mt-gd-1_55.lib'
How do I fix this?
Ive looked up the boost docs but they say nothing useful.
Also the documentation is incorrect :
http://www.boost.org/doc/libs/1_55_0/more/getting_started/windows.html#simplified-build-from-source
it says you have to type into bootstrap.bat, while it takes no input, you cant type, I checked the code, it just installs some .c files or something...
The more I read about this, the I am confused.
Very beginner-unfriendly, them libs =c
Last edited on
There are a few reasons that you can get that error. First make sure that lib is in your MSVS IDE's search directory. Then make sure you are only linking the debug, multi-threaded version of your project to this lib. Is there anymore to the error? Like "File Not Found" or "Wrong Version"?
Hmm, that was a while ago, can't remember.
I solved the problem, but I got a new problem now.
Topic archived. No new replies allowed.