According to your code, the only time you even reach the operators 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 this is because your code, which should only read the first answer
while((amountRead = isr_reader.read(streamBuf)) > 0) { receivedData.append(streamBuf, 0, amountRead); }
will be blocked until the server completes the output stream (i.e., when read returns -1 ) or until the read timeout in the socket expires. In the case of a read timeout, an exception will be thrown, and you wonβt even be able to send a second request.
The problem with HTTP responses is that they do not tell you how many bytes are read from the stream until the end of the response. This is not very important for HTTP 1.0 responses, because the server just closes the connection after the response, which allows you to get the response (status bar + headers + body) by simply reading everything to the end of the stream.
With persistent HTTP 1.1 connections, you can no longer just read everything to the end of the stream. First you need to read the status bar and headers, in turn, and then based on the status code and headers (for example, Content-Length) decide how many bytes to read to get the response body (if it is present in all). If you do this correctly, your read operations will be completed before the connection is closed or a timeout occurs, and you will definitely read the response sent by the server. This will allow you to send the next request and then read the second answer in exactly the same way as the first.
PS Request, request, reading can be βworkingβ in the sense that your server supports pipelining the request and, thus, receives and processes both requests, and as a result, you read both responses in the same buffer as your βfirstβ response.
PPS Make sure your PrintWriter uses US-ASCII encoding. Otherwise, depending on your system encoding, the request string and the headers of your HTTP requests may be distorted (incorrect encoding).