After reading all sort of docs on Java HttpURLConnection, I'm still quite confused as what kind of pooling it does and how it hands connections.
For example, the following code
URL url = new URL(someUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
OutputStream os = connection.getOutputStream();
InputStream is = connection.getInputStream();
/** Write something to os and flush */
/** Read from is */
os.close();
is.close();
connection.disconnect();
Do both os
and is
need to be flushed and closed for the underlying socket to be reusable?
Will connection.disconnect()
close the underlying socket (and hence make it unreusable)? Does keep-alive
affect this behavior?
If I use different URL objects, but with the same URL, will the connection
s created from them share the underlying sockets? How about when the host part of the URL is the same, but paths are different?
When will pooled connections be destroyed?
What's the system property that controls the size of the pool?
Additionally, if you could also compare the Android version with Java it would be great.
Thanks
Deprecated. it is misplaced and shouldn't have existed. HTTP Status-Code 401: Unauthorized. HTTP Status-Code 503: Service Unavailable.
The Java HttpURLConnection class is http specific URLConnection. It works for HTTP protocol only. By the help of HttpURLConnection class, you can retrieve information of any HTTP URL such as header information, status code, response code etc.
URLConnection is the base class. HttpURLConnection is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only. HttpsURLConnection is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only.
You don't. You close this one and create a new one.
- Do both
os
andis
need to be flushed and closed for the underlying socket to be reusable?
Closing the input stream is sufficient. You can't flush an input stream, and flushing an output stream before close is redundant.
- Will
connection.disconnect()
close the underlying socket (and hence make it unreusable)?
It 'acts as a hint' to close the underlying connection.
Does
keep-alive
affect this behavior?
Yes. If it isn't present, the connection must be closed.
- If I use different URL objects, but with the same URL, will the
connection
s created from them share the underlying sockets?
Yes.
How about when the host part of the URL is the same, but paths are different?
Yes.
- When will pooled connections be destroyed?
After an idle timeout.
- What's the system property that controls the size of the pool?
I'm not aware that there is one, but if there is it will be defined in the Networking Properties page which you can find via the Javadoc.
Additionally, if you could also compare the Android version with Java it would be great.
I believe that Android doesn't do pooling at all, but this should change when they switch to the OpenJDK source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With