According to your code, the only time you'll even reach the statements dealing with sending the second request is when the server closes the output stream (your input stream) after receiving/responding to the first request.
According to your code, the only time you'll even reach the statements dealing with sending the second request is when the server closes the output stream (your input stream) after receiving/responding to the first request. The reason for that is that your code that is supposed to read only the first response while((amountRead = isr_reader. Read(streamBuf)) > 0) { receivedData.
Append(streamBuf, 0, amountRead); } will block until the server closes the output stream (i.e. , when read returns -1) or until the read timeout on the socket elapses. In the case of the read timeout, an exception will be thrown and you won't even get to sending the second request.
The problem with HTTP responses is that they don't tell you how many bytes to read from the stream until the end of the response. This is not a big deal for HTTP 1.0 responses, because the server simply closes the connection after the response thus enabling you to obtain the response (status line + headers + body) by simply reading everything until the end of the stream. With HTTP 1.1 persistent connections you can no longer simply read everything until the end of the stream.
You first need to read the status line and the headers, line by line, and then, based on the status code and the headers (such as Content-Length) decide how many bytes to read to obtain the response body (if it's present at all). If you do the above properly, your read operations will complete before the connection is closed or a timeout happens, and you will have read exactly the response the server sent. This will enable you to send the next request and then read the second response in exactly the same manner as the first one.P.S.Request, request, read might be "working" in the sense that your server supports request pipelining and thus, receives and processes both request, and you, as a result, read both responses into one buffer as your "first" response.P.P. S Make sure your PrintWriter is using the US-ASCII encoding.
Otherwise, depending on your system encoding, the request line and headers of your HTTP requests might be malformed (wrong encoding).
Make sure you have a Connection: keep-alive in your request. This may be a moot point though. What kind of response is the server returning?
Are you using chunked transfer? If the server doesn't know the size of the response body, it can't provide a Content-Length header and has to close the connection at the end of the response body to indicate to the client that the content has ended. In this case, the keep-alive won't work.
If you're generating content on-the-fly with PHP, JSP etc. , you can enable output buffering, check the size of the accumulated body, push the Content-Length header and flush the output buffer.
The server is using a chunked transfer. However, since I can perform Request Reques Read, the server isn't closing the connection after it receives the first request. – Zxaos Oct 8 '08 at 16:00 I think it would greatly help with the troubleshooting of this issue if you could capture all the request and response headers and attach it to your question.
– Ates Goral Oct 8 '08 at 23:54.
HTTP isn't easy to get right. I know Commons HTTP Client can re-use connections like you're trying to do. If there isn't a specific reason for you using Sockets this is what I would recommend :).
There's an outside restriction on this project, and it's not reasonable to use anything but raw sockets, unfortunately. – Zxaos Oct 8 '08 at 16:00 1 All the same, you can look at the source for apache HttpClient and find out what's happening. – anjanb Oct 8 '08 at 18:02.
Writing your own correct client HTTP/1.1 implementation is nontrivial; historically most people who I've seen attempt it have got it wrong. Their implementation usually ignores the spec and just does what appears to work with one particular test server - in particular, they usually ignore the requirement to be able to handle chunked responses. Writing your own HTTP client is probably a bad idea, unless you have some VERY strange requirements.
Writing a simple http/1.1 client respecting the RFC is not such a difficult task. To solve the problem of the blocking i/o access where reading a socket in java, you must use java. Nio classes.
SocketChannels give the possibility to perform a non-blocking i/o access. This is necessary to send HTTP request on a persistent connection. Furthermore, nio classes will give better performances.My stress test give to following results : HTTP/1.0 (java.
Io) -> HTTP/1.0 (java. Nio) = +20% faster HTTP/1.0 (java.Io) -> HTTP/1.1 (java. Nio with persistent connection) = +110% faster.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.