最近在使用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应用的更多相关文章

  1. http调用端HttpClient、DefaultHttpClient、CloseableHttpClient

    1:说下httpClient接口和4.2.6版本后过时实例DefaultHttpClient,以及新的实例应用.  说到HTTP,脑子就冒出它的特性,基于TCP协议,简短点:说明是交互性的. 2:下面 ...

  2. DefaultHttpClient is deprecated 【Api 弃用]】

    最近在使用Apache的httpclient的时候,maven引用了最新版本4.3,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了,之前在使用4.2.3版本的时候,还没有 ...

  3. java HTTP请求 DefaultHttpClient is deprecated

    最近在使用Apache的httpclient的时候,maven引用了最新版本4.3,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了,之前在使用4.2.3版本的时候,还没有 ...

  4. 新旧apache HttpClient 获取httpClient方法

    在apache httpclient 4.3版本中对很多旧的类进行了deprecated标注,通常比较常用的就是下面两个类了. DefaultHttpClient -> CloseableHtt ...

  5. HttpClient发起Http/Https请求工具类

    <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcl ...

  6. 微信小程序后端开发流程

    微信小程序后端开发流程根据官网总结为两个步骤 1.前端调用 wx.login 返回了code,然后调用wx.getUserInfo获取到用户的昵称 头像 2.服务端根据code去微信获取openid, ...

  7. HttpClient4.5X使用-集成微服务

    HttpClient4.5X使用-集成微服务       1.什么是HttpClient HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直 ...

  8. 获取Eureka服务列表的各种场景

    一.第一类服务注册到eureka中,获取服务列表 1.基于SpringClientFactory获取服务列表 /** * <一句话功能简述> * <功能详细描述> * * @a ...

  9. CloseableHttpResponse的使用

    *************************** *这篇随手弄出来了,很急躁,有空再改 *************************** 基本逻辑是: 1.定义一个客户端 2.定义一个方法 ...

随机推荐

  1. Week2-作业1:阅读与博客

    Week2-作业1:阅读与博客 第一章 :概论 1. 原文如下: 移山公司程序员阿超的宝贝儿子上了小学二年级,老师让家长每天出30道加减法题目给孩子做.阿超想写一个小程序来做这件事,具体实现可以采用很 ...

  2. 团队作业7——第二次项目冲刺(Beta版本12.04——12.07)

    1.当天站立式会议照片 本次会议在5号公寓3楼召开,本次会议内容:①:熟悉每个人想做的模块.②:根据项目要求还没做的完成. 2.每个人的工作 经过会议讨论后确定了每个人的分工 组员 任务 陈福鹏 实现 ...

  3. js中的let\var\const

    在JavaScript中有三种声明变量的方式:var.let.const.下文给大家介绍js中三种定义变量的方式const, var, let的区别. 1.const定义的变量不可以修改,而且必须初始 ...

  4. iOS 判断当前app版本,升级

    要获取当前app store上的最新的版本,有两种方法, 一.在某特定的服务器上,发布和存储app最新的版本信息,需要的时候向该服务器请求查询. 二.从app store上查询,可以获取到app的作者 ...

  5. LeetCode题解:(139) Word Break

    题目说明 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, dete ...

  6. psp本周

    四人项目: 日期 类别 内容 开始时间 结束时间 中断时间 净时间 10.4 站立会议 站立会议报告 21:12 21:37 0 25 结对项目: 日期 类别 内容 开始时间 结束时间 中断时间 净时 ...

  7. ucontext-人人都可以实现的简单协程库

    ucontext的介绍 http://blog.csdn.net/qq910894904/article/details/41911175 协程的介绍 https://en.wikipedia.org ...

  8. SpringBoot(五)_表单验证

    SpringBoot(五)_表单验证 参数校验在我们日常开发中非常常见,最基本的校验有判断属性是否为空.长度是否符合要求等,在传统的开发模式中需要写一堆的 if else 来处理这些逻辑,很繁琐,效率 ...

  9. 二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离

    数据结构中一直对二叉树不是很了解,今天趁着这个时间整理一下 许多实际问题抽象出来的数据结构往往是二叉树的形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显 ...

  10. Spring Cloud与微服务构建:微服务简介

    Spring Cloud与微服务构建:微服务简介 单体架构及其不足 1.单体架构简介 在软件设计中,经常提及和使用经典的3曾模型,即表示层.业务逻辑层和数据访问层. 表示层:用于直接和用户交互,也成为 ...