`

URLConnection and HTTPClient 比较

 
阅读更多

 

 

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. Under Netscape 3.0 you can read headers only if the resource was returned with a Content-length header; if no Content-length header was returned, or under previous versions of Netscape, or using the JDK no headers can be read.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.
分享到:
评论

相关推荐

    HttpClient以及获取页面内容应用

    压缩包中含有多个文档,从了解httpclient到应用。 httpClient 1httpClint 1.1简介 HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持...

    Retrofit+OkHttp+Gson+okhttp-urlconnection-2.5.0

    OkHttp-urlconnection-2.5.0.jar是OkHttp与Java内置URLConnection的桥接库,使得OkHttp可以更好地与旧的Java网络API集成。 再者,Gson是Google提供的一个Java库,用于将Java对象转换为JSON字符串,反之亦然。在...

    tarena01 代码

    此外,URL、URLConnection和HttpClient类可以帮助我们处理网络请求和响应。 3. **Swing**: Swing是Java GUI(图形用户界面)库的一部分,用于构建桌面应用。它包含一系列组件,如JFrame、JButton、JLabel等,以及...

    android 网络连接处理分析

    使用HttpClient使用WebView 创建URL,并使用URLConnection/HttpURLConnection java.net.*下面提供了访问 HTTP 服务的基本功能。使用这部分接口的基本操作主要包括: 创建 URL 以及 URLConnection / ...

    Internet Filesi_JAASjava_

    一个可能的实现是,这个脚本首先使用Java的HTTP客户端库(如`java.net.URLConnection`或第三方库如Apache HttpClient)从互联网上下载文件,然后利用JAAS进行用户身份验证,确保只有经过验证的用户才能访问或解压...

    java jdk实列宝典 光盘源代码

    读写Blob数据,blob数据常以二进制形式存储比较大的文件数据,如图片、视频文件等,本文介绍如何往数据库中读写blob数据,BlobData.java; 使用ResultSet更新数据库,UpdateWithResultSet.java; 使用RowSet,....

    MC0037:网络编程知识库

    Java提供了一系列的类和接口,如Socket、ServerSocket、URL和URLConnection,它们为开发者提供了构建网络应用程序的基础。Socket类用于创建客户端连接,而ServerSocket则用于服务器端监听客户端的连接请求。 2. **...

    xUtils3 demo

    HTTP实现替换HttpClient为UrlConnection, 自动解析回调泛型, 更安全的断点续传策略. 支持标准的Cookie策略, 区分domain, path. 事件注解去除不常用的功能, 提高性能. 数据库api简化提高性能, 达到和greenDao一致的...

Global site tag (gtag.js) - Google Analytics