HttpURLConnection.getResponseCode() documentation comment

boris.x.shukhat at boris.x.shukhat at
Wed Aug 3 17:55:55 UTC 2016

To whom it may concern,

This comment is about API documentation on HttpURLConnection.getResponseCode() and it is based on the implementation details as well.

HttpURLConnection implementation caches response code that allows to repeatedly get a response code from HttpURLConnection  even after disconnect() is called. If a response code had been cached before disconnect() was called then its value is returned. But if the return code has not been cached, it (under some circumstances) opens a connection and leaves it opened if disconnect() is not called again. If called in the loop multiple times this design potentially can cause connection leakage and it happens in open source library
lines 397-406

I do not think that it is reasonable to change the implementation at this point because it can break existing software but it has to be reflected in API help on HttpURLConnection.getResponseCode() that it can leave connection open and another disconnect() may need to be called.

The problem can be reproduced by running the following code long enough and using ss Linux command (netstat command on Windows) to periodically monitor sockets with established connection.

public class Test
    public static void main(String...args) throws Exception
            URL url=new URL("...");
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            int responseCode=connection.getResponseCode();


Boris Shukhat


This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at:

For important disclosures, please see: regarding market commentary from Barclays Sales and/or Trading, who are active market participants; and in respect of Barclays Research, including disclosures relating to specific issuers, please see


More information about the core-libs-dev mailing list