Java URLConnection、HttpURLConnection 和 HttpsURLConnection 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3920419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between URLConnection, HttpURLConnection and HttpsURLConnection
提问by Questions
What is the difference between URLConnection
, HttpURLConnection
and HttpsURLConnection
(with SSL). Under what conditions, which one should I use?
是什么区别URLConnection
,HttpURLConnection
和HttpsURLConnection
(使用SSL)。在什么情况下,我应该使用哪一种?
采纳答案by user207421
URLConnection
is the base class.
URLConnection
是基类。
HttpURLConnection
is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only.
HttpURLConnection
是一个派生类,当您需要额外的 API 并且您只处理 HTTP 或 HTTPS 时,您可以使用它。
HttpsURLConnection
is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only.
HttpsURLConnection
是一个“更多派生”类,当您需要“更多额外”API 并且您只处理 HTTPS 时,您可以使用它。
All three of them are abstract, and implemented by specific classes you aren't privy to.
这三个都是抽象的,并由您不了解的特定类实现。
回答by Owen
URLConnection is an abstract class so, you could never instantiate an object of that type.
URLConnection 是一个抽象类,因此您永远无法实例化该类型的对象。
HttpURLConnection extends URLConnection and provides fields and methods specific to an HTTP URL, such as, HTTP_CLIENT_TIMEOUT or setRequestMethod.
HttpURLConnection 扩展了 URLConnection 并提供特定于 HTTP URL 的字段和方法,例如 HTTP_CLIENT_TIMEOUT 或 setRequestMethod。
HttpsURLConnection extends HttpURLConnection and provides fields and methods specific to an HTTPS URL.
HttpsURLConnection 扩展了 HttpURLConnection 并提供特定于 HTTPS URL 的字段和方法。