CloseableHttpClient与 CloseableHttpResponse应用
最近在使用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();
}
参考:http://www.yeetrack.com/?p=760
CloseableHttpClient与 CloseableHttpResponse应用的更多相关文章
- http调用端HttpClient、DefaultHttpClient、CloseableHttpClient
1:说下httpClient接口和4.2.6版本后过时实例DefaultHttpClient,以及新的实例应用. 说到HTTP,脑子就冒出它的特性,基于TCP协议,简短点:说明是交互性的. 2:下面 ...
- DefaultHttpClient is deprecated 【Api 弃用]】
最近在使用Apache的httpclient的时候,maven引用了最新版本4.3,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了,之前在使用4.2.3版本的时候,还没有 ...
- java HTTP请求 DefaultHttpClient is deprecated
最近在使用Apache的httpclient的时候,maven引用了最新版本4.3,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了,之前在使用4.2.3版本的时候,还没有 ...
- 新旧apache HttpClient 获取httpClient方法
在apache httpclient 4.3版本中对很多旧的类进行了deprecated标注,通常比较常用的就是下面两个类了. DefaultHttpClient -> CloseableHtt ...
- HttpClient发起Http/Https请求工具类
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcl ...
- 微信小程序后端开发流程
微信小程序后端开发流程根据官网总结为两个步骤 1.前端调用 wx.login 返回了code,然后调用wx.getUserInfo获取到用户的昵称 头像 2.服务端根据code去微信获取openid, ...
- HttpClient4.5X使用-集成微服务
HttpClient4.5X使用-集成微服务 1.什么是HttpClient HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直 ...
- 获取Eureka服务列表的各种场景
一.第一类服务注册到eureka中,获取服务列表 1.基于SpringClientFactory获取服务列表 /** * <一句话功能简述> * <功能详细描述> * * @a ...
- CloseableHttpResponse的使用
*************************** *这篇随手弄出来了,很急躁,有空再改 *************************** 基本逻辑是: 1.定义一个客户端 2.定义一个方法 ...
随机推荐
- JTS
在这个系列的 第 1 部分,我们讨论了事务并研究了它们的基本属性 ― 原子性(atomicity).一致性(consistency).孤立性(isolation)和持久性(durability).事务 ...
- iOS开发值得收藏的博客
http://kobedai.me/ objc.io PS:经典,内容深而广objc中国NSHipster PS:很多小细节NSHipster 中文版唐巧的技术博客 PS:LZ是唐巧的脑残粉…OneV ...
- 获取ios设备的udid
今天get的第二个技能~~~ UDID指的是设备的唯一设备识别符,ipa包未上架之前如果不添加udid是无法安装成功的.那么如何快速获取ios设备的udid呢? 今天get的方法是用蒲公英,网址:ht ...
- epel [Errno 14] problem making ssl connection
问题描述: 执行yum命令时,报错[Errno 14] problem making ssl connection 问题分析: ssl证书问题 问题解决: sed -i 's/^#baseurl/ba ...
- Hibernate 之主键生成策略小总结
主键生成策略大致分两种: 手工控制策略 自动生成策略[框架自动生成和数据库自动生成] 手工控制策略: assigned:类型是任意的,需要在 save() 到数据库前,编码人员手工设置主键值,也就是调 ...
- P3293 [SCOI2016]美味
题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1<=i<=n).有 m 位顾客,第 i 位顾客的期望值为 bi,而他的偏好值为 xi .因此,第 ...
- P2144 [FJOI2007]轮状病毒
题目描述 轮状病毒有很多变种.许多轮状病毒都是由一个轮状基产生.一个n轮状基由圆环上n个不同的基原子和圆心的一个核原子构成.2个原子之间的边表示这2个原子之间的信息通道,如图1. n轮状病毒的产生规律 ...
- ZJOI2018外省选手酱油记Day1
Day0 上午考试...又爆零了 下午讲完题后放假 然后就滚回去收拾行李准备去\(ZJ\) Day1 衢州?我怎么从来没听过这个地方..肯定是我见识少 下午 上高铁出发,\(3个小时\),看了一下电影 ...
- Beta 完结撒花 —— 事后诸葛亮
写在前面 林燊大哥 团队成员 短学号 名 2325 燊(队长) 1232 志豪 1131 喜源 2523 宏岩 2230 恺翔 2509 钧昊 2507 俞辛 2501 宇航 2502 柏涛 项目宣传 ...
- BGP的那些安全痛点(转)
0x00 BGP(RFC 1771. RFC 4271)定义 全称是Border Gateway Protocol, 对应中文是边界网关协议,最新版本是BGPv4. BGP是互联网上一个核心的互联网去 ...