Both close and disconnect methods release the connection if it is not already issued with the following two differences:
close throws method IOException . Therefore, in the first example, the signature of the downloadUrl's method has throws IOException . Where the disconnect method throws no exceptions.- Calling the
close method ensures that any future closed connection references, such as read , will result in an IOException .
The most important design fact in the implementation of Android HttpURLConnection :
The connection is made when the last bit of the response is used. When the answer is fully read, the connection will be released and will be immediately merged.
In the image below, you can see the connection and connectionReleased variables, respectively, null and true , as soon as all the data has been read. In this case, the disconnect call does not matter, the close call simply provides future calls with closed IOException links.

If data is still available in the InputStream , you must call close or disconnect to release the connection explicitly. In this case, the connection is not reused , and the underlying socket connection also closes. This is done in the expectation that additional data may appear in the InputStream.
You can see in the code snippets below that both disconnect and close will finally call httpEngine.release(false) , which will close the connection without adding a connection pool.
disable implementation:
@Override public final void disconnect() {
close :
@Override public void close() throws IOException { if (closed) { return; } closed = true; if (bytesRemaining != 0) { unexpectedEndOfInput(); } }
unexpectedEndOfInput :
protected final void unexpectedEndOfInput() { if (cacheRequest != null) { cacheRequest.abort(); } httpEngine.release(false); }
version :
public final void release(boolean reusable) { // If the response body comes from the cache, close it. if (responseBodyIn == cachedResponseBody) { IoUtils.closeQuietly(responseBodyIn); } if (!connectionReleased && connection != null) { connectionReleased = true; // We cannot reuse sockets that have incomplete output. if (requestBodyOut != null && !requestBodyOut.closed) { reusable = false; } // If the headers specify that the connection shouldn't be reused, don't reuse it. if (hasConnectionCloseHeader()) { reusable = false; } if (responseBodyIn instanceof UnknownLengthHttpInputStream) { reusable = false; } if (reusable && responseBodyIn != null) { // We must discard the response body before the connection can be reused. try { Streams.skipAll(responseBodyIn); } catch (IOException e) { reusable = false; } } if (!reusable) { connection.closeSocketAndStreams(); connection = null; } else if (automaticallyReleaseConnectionToPool) { HttpConnectionPool.INSTANCE.recycle(connection); connection = null; } }
Summary:
- A connection is automatically issued to the connection pool as soon as the last bit of the response is used.
- If an
IOException will be handled by the method acting on the InputStream , use disconnect . If an IOException will be thrown by the caller of a method running on an InputStream , use close . Remember close ensures that an IOException will be thrown when the read operation is performed on a closed connection in the future.