Java HTTP/1.1 GET request BufferedReader readLine never stops

Dirkx Senne :

Hello I'm making an HTTP client. I'm trying to fetch google.com's html code. I have a problem the the BufferedReader.readLine() function is blocking endlessly because the remote server apparently doesn't send a blank line? Or could it be that my request is wrong?

Appreciate any help!

public static void main(String[] args) {
        String uri = "www.google.com";
        int port = 80;
        Socket socket = new Socket(uri, port);
        PrintWriter toServer = new PrintWriter(socket.getOutputStream(), true);
        InputStream inputStream = socket.getInputStream();
        get(uri, port, language, socket, toServer, inputStream);
}

public static void get(String uri, int port, String language, Socket socket, PrintWriter toServer, InputStream inputStream) {
        try {
            toServer.println("GET / HTTP/1.1");
            toServer.println("Host: " + uri + ":" + port);
            toServer.println();

            // Parse header
            StringBuilder stringBuilder = new StringBuilder();

            BufferedReader fromServer = new BufferedReader(new InputStreamReader(inputStream));

            String line;
                while ((line = fromServer.readLine()) != null) {
                    stringBuilder.append(line);
                }
            System.out.println("done");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
Steffen Ullrich :

You are sending a HTTP/1.1 request which by default enables HTTP keep-alive. This means that the server might keep the TCP connection open after the response was sent in order to accept more requests from the client. Your code instead assumes that the server will close the connection after the response was finished by explicitly expecting readline to return null. But since the server will not close the connection (or only after some long timeout) the readline will just block.

To fix this either use HTTP/1.0 (which has keep-alive off by default) instead of HTTP/1.1 or explicitly tell the server that no more requests will be send by adding a Connection: close header.

Please note that in general HTTP is way more complex than you might think if you've just seen a few examples. The problem you face in your question is only a glimpse into more problems which you will face when continuing this path. If you really want to implement your own HTTP handling instead of using established libraries please study the actual standard instead of just assuming a specific behavior.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=276921&siteId=1