DefaultHttpClient is deprecated 【Api 弃用]】
最近在使用Apache的httpclient的时候,maven引用了最新版本4.3,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了,之前在使用4.2.3版本的时候,还没有被deprecated。去看了下官方文档,确实不推荐使用了,点击此处详情。
- DefaultHttpClient —> CloseableHttpClient
- HttpResponse —> CloseableHttpResponse
官方给出了新api的样例,如下。
Get方法:
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
// The underlying HTTP connection is still held by the response object
// to allow the response content to be streamed directly from the network socket.
// In order to ensure correct deallocation of system resources
// the user MUST either fully consume the response content or abort request
// execution by calling CloseableHttpResponse#close().
//建立的http连接,仍旧被response1保持着,允许我们从网络socket中获取返回的数据
//为了释放资源,我们必须手动消耗掉response1或者取消连接(使用CloseableHttpResponse类的close方法)
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity1);
} finally {
response1.close();
}
Post方法:
HttpPost httpPost = new HttpPost("http://targethost/login");
//拼接参数
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
//消耗掉response
EntityUtils.consume(entity2);
} finally {
response2.close();
}
再往下看HttpClients的源码,具体的实现都在HttpClientBuilder
的build
方法中,有兴趣的可以去apache看源码。
/**
* Creates {@link CloseableHttpClient} instance with default
* configuration.
*/
public static CloseableHttpClient createDefault() {
return HttpClientBuilder.create().build();
}
DefaultHttpClient is deprecated 【Api 弃用]】的更多相关文章
- Android系统编译错误Note: Some input files use or override a deprecated API. 解决办法【转】
本文转载自:http://blog.csdn.net/lilidejing/article/details/46564491 进入系统framework层修改了下MediaPlayer.java的源码 ...
- java HTTP请求 DefaultHttpClient is deprecated
最近在使用Apache的httpclient的时候,maven引用了最新版本4.3,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了,之前在使用4.2.3版本的时候,还没有 ...
- HttpClient 模拟发送Post和Get请求 并用fastjson对返回json字符串数据解析,和HttpClient一些参数方法的deprecated(弃用)的综合总结
最近在做一个接口调用的时候用到Apache的httpclient时候,发现引入最新版本4.5,DefaultHttpClient等老版本常用的类已经过时了,不推荐使用了:去官网看了一下在4.3之后就抛 ...
- Dart 编写Api弃用警告
例如body2在以后的版本将被bodyText1代替 @Deprecated( 'This is the term used in the 2014 version of material desig ...
- Java 9 揭秘(15. 增强的弃用注解)
Tips 做一个终身学习的人. 主要介绍以下内容: 如何弃用API @deprecate Javadoc标签和@Deprecation注解在弃用的API中的角色 用于生成弃用警告的详细规则 在JDK ...
- 如何正确地使用Java的@deprecated标注
没有什么事情比看到一个没有任何说明的@deprecated标注更让人愤怒的事情了.这种做法只能让人困惑,我到底还要不要用这个已经‘废弃’的方法?如果开发者不希望某个方法再被人用的话,就要好好地为@de ...
- 【译】Android API 规范
[译]Android API 规范 译者按: 修改R代码遇到Lint tool的报错,搜到了这篇文档,aosp仓库地址:Android API Guidelines. 58e9b5f Project ...
- .NET 6当中的Web API版本控制
大家好,我是张飞洪,感谢您的阅读,我会不定期和你分享学习心得,希望我的文章能成为你成长路上的垫脚石,让我们一起精进. 为了了解ASP.NET Core Web API的版本控制,我们必须了解API中的 ...
- 好RESTful API的设计原则
说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间,如有人愿意转载请注明出处,谢谢^_^ P ...
随机推荐
- zTree 勾选checkbox
zTree 勾选checkbox var setting = { check: { enable: true// chkboxType : { "Y&quo ...
- jquery学习笔记3 jq遍历
遍历方式:向上(父级元素) 向下(子元素) 水平(同胞元素) 一.向上遍历 parent() 向上一级 放回被选元素的直接父元素 parents() 返回被选元 ...
- 关于autoconf
1 the difference between AC_ARG_ENABLE and AC_ARG_WITH AC_ARG_ENABLE是enable一个feature,该feature所对应的源码包 ...
- EasyuiAPI:菜单
一.LinkButton(按钮) 1.通过标签创建: <a id="btn" href="#" class="easyui-linkbutton ...
- 如何自定义JSR-303标准的validator
在web应用中为了保证数据的有效性而对用户提交的表单数据是必需的,而前台客户端的验证例如javascript并不总是那么安全和可靠,这样我们就需要一个健壮的后台验证框架来处理这个问题.好在java发布 ...
- aspnet5安装ef7备忘
1.安装kvm 首先,你需要以管理员权限打开cmd,执行如下的脚本: @powershell -NoProfile -ExecutionPolicy unrestricted -Command &qu ...
- HDU 1863 Kruskal求最小生成树
好久没写博客了写着玩的…… Kruskal这种东西离散都学过…… 一句话…… 添加当前图权值最小且构不成环的一条边 直到连接所有点…… 其他人好多Kruskal的模版 肯定有比我的好的…… 就是刷一波 ...
- GitHub赠送DigitalOcean优惠码100美元
著名的项目托管网站GitHub本周联合DigitalOcean VPS和NameCheap送给学生福利了!十余种产品免费拿!包括免费赠送digitalocean优惠码价值100美元!以及nameche ...
- 洛谷-三连击(升级版)-BOSS战-入门综合练习1
题目描述 Description 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数构成A:B:C的比例,试求出所有满足条件的三个三位数,若无解,输出“No!!!”. 输入输出格 ...
- Openjudge-计算概论(A)-点与正方形的关系
描述: 有一个正方形,四个角的坐标(x,y)分别是(1,-1),(1,1),(-1,-1),(-1,1),x是横轴,y是纵轴.写一个程序,判断一个给定的点是否在这个正方形内.输入输入坐标x,y输出ye ...