从Java套接字InputStream读取请求内容始终在标头之后挂起

问题描述:

我正在尝试使用核心Java从输入流中读取HTTP请求数据,使用以下代码:

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();

我收到标题很好,但是客户端只是永远挂起,因为服务器永远不会找到EOF请求。我该如何处理?我已经看到这个问题相当多了,而且大多数解决方案都涉及到类似上面的内容,但它并不适用于我。我尝试使用curl和Web浏览器作为客户端,只是发送一个get请求

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

感谢您的任何想法

HTTP请求以空行结束(可选地后跟请求数据,如表单数据或文件上载),而不是EOF。你想要这样的东西:

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();