1
votes

I am writing a browser that sends http get requests constructed the same way browsers do. I want to test the "keep-alive" header so I send and receive the following package twice:

"POST /test.php HTTP1.1\r\n"
"Host: 192.168.0.1:8000\r\n"
"Connection: keep-alive\r\n"
"Content-Length: 17\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Accept-Language: en-US,en;q=0.8\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"Referer: http://192.168.0.1:8000/test.php\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n"
"\r\n\r\n"
"para:test_tst_tst"

However, when i receive the second time, I get error 10054 (windows error). It seems as though the server is refusing to send me data.

What this wrong with this?

1
Did you check the response from the server, that its header also had keep-alive set? Also remember that the timeout on the server may be only a few seconds.Some programmer dude
You're missing a "/" in the first line of your request for "HTTP/1.1". You've also got an extra "\r\n" before your content--you have two blank lines instead of just one. Thus the actual content length of your body is 19, and not 17; the server then probably tries to parse the next request as if it began with "st", the extra two characters off the end of the first request.Jon Moore
What have you tried? The server seems to be on your local network, so you should be able to check its logs. Do you see any errors? What is the response of the first request? What does a tool like Fiddler say about your request(s), are they valid (seems not so, according to the other commenters)? Is the server configured to never honor keepalives (since it may ignore the header and respond with a Connection: close and do so)?CodeCaster

1 Answers

1
votes

I guess the error 10054 you are talking about is the Winsock error "Connection reset by peer".

Your problem is that you have 3 \r\n pairs between http header and body, rather than just 2

"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n"
"\r\n\r\n"
"para:test_tst_tst"

Quoting from the spec:

generic-message = start-line
                  *(message-header CRLF)
                  CRLF
                  [ message-body ]

So the server will read the third \r\n pair as the first 2 bytes of the body, and then what remains of the 17 bytes of body length as specified in the Content-Length header. So rather than starting with POST, your second request will be interpreted as starting with stPOST, that is what's probably confusing the server.

Remove one \r\n pair and you should be fine.

Btw, Keep-Alive is the default value for the Connection-Type in HTTP 1.1, so you don't even need to specify that explicitly.