Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between URLConnection, HttpURLConnection and HttpsURLConnection

What is the difference between URLConnection, HttpURLConnection and HttpsURLConnection (with SSL). Under what conditions, which one should I use?

like image 434
Questions Avatar asked Oct 13 '10 02:10

Questions


People also ask

When should I use HttpURLConnection?

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.

What is URLConnection?

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.

Can HttpURLConnection be used with https?

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.

Is HttpURLConnection thread safe?

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.


2 Answers

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.

like image 184
user207421 Avatar answered Sep 21 '22 10:09

user207421


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.

like image 28
Owen Avatar answered Sep 22 '22 10:09

Owen