java 使用 HTTPClient 还是 HttpUrlConnection?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1739548/
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
Use HTTPClient or HttpUrlConnection?
提问by Marcus Leon
We're implementing a REST client on JRE 1.4.
我们正在 JRE 1.4 上实现 REST 客户端。
Seems two good options for a client REST framework are HttpClientand HttpUrlConnection.
客户端 REST 框架的两个不错的选择似乎是HttpClient和HttpUrlConnection。
Is there a reason to use HttpClient over the JRE's HttpUrlConnection?
是否有理由在 JRE 的 HttpUrlConnection 上使用 HttpClient?
回答by SteveD
I'll give you a single, concrete reason to favour Apache's HTTPClient over the JDK implementation: The JDK's HttpUrlConnectiondoesn't support timeouts*, Apache's HTTPClient does.
我将给您一个支持 Apache 的 HTTPClient 而非 JDK 实现的具体原因:JDKHttpUrlConnection不支持超时*,Apache 的 HTTPClient 支持。
Applications should always have the ability to set timeouts when calling into other systems (databases, remote services, your own server backend, ...).
应用程序应始终能够在调用其他系统(数据库、远程服务、您自己的服务器后端等)时设置超时。
* This was fixed in Java 1.5; Java 1.5 and higher support timeouts in HttpUrlConnection.
* 这是在 Java 1.5 中修复的;Java 1.5 和更高版本支持 HttpUrlConnection 中的超时。
回答by Jim Ferrans
I would recommend Jakarta Commons HTTP Client over java.net.HttpUrlConnection as it is more mature and has a richer feature set. For example you can ask it to set up multi-threaded connection pool (see MultiThreadedHttpConnectionManager), and it has full support for all the HTTP methods (GET, PUT, POST, DELETE, OPTIONS, TRACE).
我会推荐 Jakarta Commons HTTP Client 而不是 java.net.HttpUrlConnection,因为它更成熟并且具有更丰富的功能集。例如,您可以要求它设置多线程连接池(参见MultiThreadedHttpConnectionManager),它完全支持所有 HTTP 方法(GET、PUT、POST、DELETE、OPTIONS、TRACE)。
回答by Jerome Louvel
The Restlet Frameworkalso has an API which works both server-side and client-side. We support pluggable client connectors, leveraging HttpURLConnection or Apache HTTP Client or our own internal HTTP client.
在Restlet框架也有其作品既服务器端和客户端的API。我们支持可插入的客户端连接器,利用 HttpURLConnection 或 Apache HTTP 客户端或我们自己的内部 HTTP 客户端。
Our ClientResource class provides a higher level HTTP client API, with features like automatic redirection, transparent conversion between objects and representations, content negotiation and more.
我们的 ClientResource 类提供了更高级别的 HTTP 客户端 API,具有自动重定向、对象和表示之间的透明转换、内容协商等功能。
Best regards,
最好的祝福,
Jerome Louvel
杰罗姆·卢维尔
Restlet ~ Founder and Lead developer ~ http://www.restlet.org
Restlet ~ 创始人和首席开发人员 ~ http://www.restlet.org
Noelios Technologies ~ Co-founder ~ http://www.noelios.com
Noelios Technologies ~ 联合创始人 ~ http://www.noelios.com
回答by helmut
... httpclient does not support kerberos/ntlm authentication for proxies etc... java's httpurlconnection will do authentication out of the box...
... httpclient 不支持代理等的 kerberos/ntlm 身份验证... java 的 httpurlconnection 将进行开箱即用的身份验证...
回答by Aditya
In my experience HttpClient is slightly easier and more intuitive to use than using HttpUrlConnection, but I think it's a very subjective decision and YMMV.
根据我的经验,使用 HttpClient 比使用 HttpUrlConnection 更容易和更直观,但我认为这是一个非常主观的决定和 YMMV。
回答by Eric
I'd go with the JRE version just so I would have one less dependency to ship around.
我会使用 JRE 版本,这样我就可以减少一个依赖项。
回答by jonaspp
The HttpUrlConnection is easy to handle. REST implementations are quite simple.
HttpUrlConnection 很容易处理。REST 实现非常简单。
Although you must consider the whole environment about this implementation and check what will work better for you.
尽管您必须考虑有关此实现的整个环境,并检查哪种方法更适合您。

