HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。
HttpURLConnection是java的标准类
HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient,更多使用 HttpClient 的应用可以参见http://wiki.apache.org/jakarta-httpclient/HttpClientPowered。HttpClient 项目非常活跃,使用的人还是非常多的。目前 HttpClient 版本是在 2005.10.11 发布的 3.0 RC4 。
HttpClient是个很不错的开源框架,封装了访问http的请求头,参数,内容体,响应等等,
HttpURLConnection是java的标准类,什么都没封装,用起来太原始,不方便,比如重访问的自定义,以及一些高级功能等。
推荐HttpClient!!
| URLConnection | HTTPClient | |
|---|---|---|
| Proxies and SOCKS | Full support in Netscape browser, appletviewer, and applications (SOCKS: Version 4 only); no additional limitations from security policies. | Full support (SOCKS: Version 4 and 5); limited in applets however by security policies; in Netscape can't pick up the settings from the browser. |
| Authorization | Full support for Basic Authorization in Netscape (can use info given by the user for normal accesses outside of the applet); no support in appletviewer or applications. | Full support everywhere; however cannot access previously given info from Netscape, thereby possibly requesting the user to enter info (s)he has already given for a previous access. Also, you can add/implement additional authentication mechanisms yourself. |
| Methods | Only has GET and POST. | Has HEAD, GET, POST, PUT, DELETE, TRACE and OPTIONS, plus any arbitrary method. |
| Headers | Currently you can only set any request headers if you are doing a POST under Netscape; for GETs and the JDK you can't set any headers. | Allows any arbitrary headers to be sent and received. |
| Automatic Redirection Handling | Yes. | Yes (as allowed by the HTTP/1.1 spec). |
| Persistent Connections | No support currently in JDK; under Netscape uses HTTP/1.0 Keep-Alive's. | Supports HTTP/1.0 Keep-Alive's and HTTP/1.1 persistence. |
| Pipelining of Requests | No. | Yes. |
| Can handle protocols other than HTTP | Theoretically; however only http is currently implemented. | No. |
| Can do HTTP over SSL (https) | Under Netscape, yes. Using Appletviewer or in an application, no. | No (not yet). |
| Source code available | No. | Yes. |
本文对比了Java中标准类HttpURLConnection与开源框架HttpClient的功能特性。HttpClient提供了更高效、丰富的HTTP协议支持,包括对最新协议的支持及多种HTTP方法。此外,还讨论了两者在代理设置、授权方式、方法支持等方面的差异。

被折叠的 条评论
为什么被折叠?



