最近在使用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. DocX插件

    DocX是一个用C#编写的.NET库,它允许开发人员以简单直观的方式操作Word文件.

  2. Nginx服务器搭建

    http://blog.csdn.net/molingduzun123/article/details/51850925 http://tengine.taobao.org/book/index.ht ...

  3. JabRef学习笔记(一)

    JabRef简介 JabRef is an open source bibliography reference manager. The native file format used by Jab ...

  4. syntax error:unexpected end of file

    将window上编辑的xxy1.sh脚本上传到linux上,并执行的时候提示 xxy1.sh: line 17: syntax error: unexpected end of file 但是通过ca ...

  5. Python3.6 AES加密 pycrypto‎ 更新为 pycrypto‎demo | TypeError: Object type <class 'str'> cannot be passed to C code

    #!/usr/bin/env python# -*- coding:utf-8 -*-# @author: rui.xu# @update: jt.huang# 这里使用pycrypto‎demo库# ...

  6. ie6 ie7 userdata 本地存储 引发的惨案.

    我使用 documentElement 作为userdata 作为本地存储的载体. document.documentElement.addBehavior("#default#userda ...

  7. JavaWeb基础【1】—— Tomcat

    此笔记是学习黑马程序员JavaWeb系列视频的课堂笔记. 感谢黑马程序员. 一.Tomcat概述 Tomcat服务器由Apache提供,开源免费.由于Sun和其他公司参与到了Tomcat的开发中,所以 ...

  8. Memcache服务器端+Redis服务器端+PHP Memcache扩展+PHP Memcached扩展+PHP Redis扩展+MemAdmin Memcache管理工具+一些概念(更新中)

    Memcache和Redis因为操作简单,是我们常用的服务器数据缓存系统,以下文字仅作备忘记录,部份转载至网络. 一.定义 1.Memcache Memcache是一个高性能的分布式的内存对象缓存系统 ...

  9. enginefuncs_t 结构体中的函数

    就是常见的 g_engfuncs 中的函数.AMXX 里就是 fakemeta 的 EngFunc_** // 这些函数由引擎提供给EXTDLL使用.mp.dll hl.dll ... typedef ...

  10. BZOJ3142 HNOI2013数列(组合数学)

    考虑差分序列.每个差分序列的贡献是n-差分序列的和,即枚举首项.将式子拆开即可得到n*mk-1-Σi*cnt(i),cnt(i)为i在所有差分序列中的出现次数之和.显然每一个数出现次数是相同的,所以c ...