What is the difference between URLConnection
, HttpURLConnection
and HttpsURLConnection
(with SSL). Under what conditions, which one should I use?
The method is used to enable streaming of a HTTP request body without internal buffering, when the content length is not known in advance. It sets whether HTTP redirects (requests with response code) should be automatically followed by HttpURLConnection class.
URLConnection is an abstract class whose subclasses form the link between the user application and any resource on the web. We can use it to read/write from/to any resource referenced by a URL object. There are mainly two subclasses that extend the URLConnection class.
HttpURLConnection class is an abstract class directly extending from URLConnection class. It includes all the functionality of its parent class with additional HTTP-specific features. HttpsURLConnection is another class that is used for the more secured HTTPS protocol.
it's not thread safe. you shouldn't cache/share a connection. just create a new connection for each request. there is certainly a little overhead in creating new connections, but it is very small, you shouldn't worry about it.
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.
All three of them are abstract, and implemented by specific classes you aren't privy to.
URLConnection is an abstract class so, you could never instantiate an object of that type.
HttpURLConnection extends URLConnection and provides fields and methods specific to an HTTP URL, such as, HTTP_CLIENT_TIMEOUT or setRequestMethod.
HttpsURLConnection extends HttpURLConnection and provides fields and methods specific to an HTTPS URL.
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