Hoofamon Hoofamon - 15 days ago 5
Java Question

Reading request content from Java socket InputStream, always hangs after header

I am trying to use core Java to read HTTP request data from an inputstream, using the following code:

BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();


I receive the header fine, but then the client just hangs forever because the server never finds "EOF" of the request. How do I handle this? I've seen this question asked quite a bit, and most solutions involve something like the above, however it's not working for me. I've tried using both curl and a web browser as the client, just sending a get request

Thanks for any ideas

Answer

An HTTP request ends with a blank line (optionally followed by request data such as form data or a file upload), not an EOF. You want something like this:

BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while (!(inputLine = in.readLine()).equals(""))
    System.out.println(inputLine);
in.close();