38

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

Stevoisiak
  • 16,510
  • 19
  • 94
  • 173
Questions
  • 17,755
  • 29
  • 68
  • 100

2 Answers2

61

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.

Stevoisiak
  • 16,510
  • 19
  • 94
  • 173
user207421
  • 289,834
  • 37
  • 266
  • 440
8

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.

Owen
  • 19,917
  • 13
  • 38
  • 47