Android网络HttpURLConnection和HttpClient
2016-04-18 15:14
423 查看
1.概念
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的
Java 应用程序需要直接通过 HTTP 协议来访问网络资源。在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能:HttpURLConnection。但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。
除此之外,在Android中,androidSDK中集成了Apache的HttpClient模块,用来提供高效的、最新的、功能丰富的支持 HTTP 协议工具包,并且它支持 HTTP 协议最新的版本和建议。使用HttpClient可以快速开发出功能强大的Http程序。
2.区别
HttpClient是个很不错的开源框架,封装了访问http的请求头,参数,内容体,响应等等,
HttpURLConnection是java的标准类,什么都没封装,用起来太原始,不方便,比如重访问的自定义,以及一些高级功能等。
3.案例
URLConnection
?
HTTPClient
?
servlet端json转化:
?
Android端接收:
?
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的
Java 应用程序需要直接通过 HTTP 协议来访问网络资源。在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能:HttpURLConnection。但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。
除此之外,在Android中,androidSDK中集成了Apache的HttpClient模块,用来提供高效的、最新的、功能丰富的支持 HTTP 协议工具包,并且它支持 HTTP 协议最新的版本和建议。使用HttpClient可以快速开发出功能强大的Http程序。
2.区别
HttpClient是个很不错的开源框架,封装了访问http的请求头,参数,内容体,响应等等,
HttpURLConnection是java的标准类,什么都没封装,用起来太原始,不方便,比如重访问的自定义,以及一些高级功能等。
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. |
URLConnection
?
?
?
?
相关文章推荐
- 神经网络学习笔记-问题记录(三)
- TCP/IP协议
- Http和Https的区别
- CentOS报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock32 error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
- <a target=_blank href=http://www.codeforge.cn/point/?recoid=1565262>CodeForge源码分享</a>
- POJ_1459_Power Network_网络流问题
- 深圳证券交易所网络投票业务指南
- 根据HttpSevletRequest 获取url 所有参数
- 阿里云ubuntu配置安装nginx,配置域名和HTTPS
- Win7提示诊断策略服务未运行无法诊断检查网络解决方法
- 计算机网络: 同步传输和异步传输(理解)
- 浅谈HTTP中Get与Post的区别
- 深度学习方法:受限玻尔兹曼机RBM(二)网络模型
- tomcat设置密码,访问 http://localhost:8080/manager/html,要密码
- cas系列(三)--HTTP和HTTPS、SSL
- http块指令解析以及存储
- hdu2732 网络流经典建图
- 详解https是如何确保安全的?
- HttpClient使用讲解
- 两步搞定 Tomcat 下启用 https:// 访问