使用 HTTPClient 还是 HttpUrlConnection?

发布于 2024-08-11 05:35:00 字数 1431 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

他夏了夏天 2024-08-18 05:35:00

我将给您一个支持 Apache 的 HTTPClient 而不是 JDK 实现的具体理由:JDK 的 HttpUrlConnection 不支持超时*,而 Apache 的 HTTPClient 支持。

应用程序应该始终能够在调用其他系统(数据库、远程服务、您自己的服务器后端……)时设置超时。

* 这已在 Java 1.5 中修复; Java 1.5 及更高版本支持 HttpUrlConnection 中的超时。

I'll give you a single, concrete reason to favour Apache's HTTPClient over the JDK implementation: The JDK's HttpUrlConnection doesn't support timeouts*, Apache's HTTPClient does.

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.

十级心震 2024-08-18 05:35:00

我会推荐 Jakarta Commons HTTP Client 而不是 java.net.HttpUrlConnection,因为它更成熟并且具有 更丰富的功能集。例如,您可以要求它设置多线程连接池(请参阅 MultiThreadedHttpConnectionManager< /a>),并且它完全支持所有 HTTP 方法(GET、PUT、POST、DELETE、OPTIONS、TRACE)。

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).

随风而去 2024-08-18 05:35:00

Restlet Framework 还有一个可在服务器端和客户端工作的 API。我们支持可插入客户端连接器,利用 HttpURLConnection 或 Apache HTTP 客户端或我们自己的内部 HTTP 客户端。

我们的 ClientResource 类提供了更高级别的 HTTP 客户端 API,具有自动重定向、对象和表示之间的透明转换、内容协商等功能。

谨致问候,

Jerome Louvel

Restlet ~ 创始人兼首席开发人员 ~ http://www.restlet.org

Noelios Technologies ~联合创始人 ~ http://www.noelios.com

The Restlet Framework also 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.

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.

Best regards,

Jerome Louvel

Restlet ~ Founder and Lead developer ~ http://www.restlet.org

Noelios Technologies ~ Co-founder ~ http://www.noelios.com

雨落□心尘 2024-08-18 05:35:00

根据我的经验,HttpClient 比使用 HttpUrlConnection 更容易使用,也更直观,但我认为这是一个非常主观的决定,YMMV。

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.

北恋 2024-08-18 05:35:00

我会选择 JRE 版本,这样我就可以减少一个依赖项。

I'd go with the JRE version just so I would have one less dependency to ship around.

何以笙箫默 2024-08-18 05:35:00

... httpclient 不支持代理等的 kerberos/ntlm 身份验证...
java 的 httpurlconnection 将进行开箱即用的身份验证...

... httpclient does not support kerberos/ntlm authentication for proxies etc...
java's httpurlconnection will do authentication out of the box...

无戏配角 2024-08-18 05:35:00

HttpUrlConnection 很容易处理。 REST 实现非常简单。

尽管您必须考虑此实施的整个环境并检查什么对您更有效。

The HttpUrlConnection is easy to handle. REST implementations are quite simple.

Although you must consider the whole environment about this implementation and check what will work better for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文