什么是httpclient
  HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
下载地址:http://hc.apache.org/
使用范例

  1. public class HttpClientTest {
  2. @Test
  3. public void doGet() throws Exception {
  4. //创建一个httpclient对象
  5. CloseableHttpClient httpClient = HttpClients.createDefault();
  6. //创建一个GET对象
  7. HttpGet get = new HttpGet("http://www.sogou.com");
  8. //执行请求
  9. CloseableHttpResponse response = httpClient.execute(get);
  10. //取响应的结果
  11. int statusCode = response.getStatusLine().getStatusCode();
  12. System.out.println(statusCode);//返回200
  13. HttpEntity entity = response.getEntity();
  14. String string = EntityUtils.toString(entity, "utf-8");
  15. System.out.println(string);//返回sogou主页的页面代码
  16. //关闭httpclient
  17. response.close();
  18. httpClient.close();
  19. }
  20. @Test
  21. public void doGetWithParam() throws Exception{
  22. CloseableHttpClient httpClient = HttpClients.createDefault();
  23. //创建一个uri对象
  24. URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");
  25. uriBuilder.addParameter("query", "花千骨");
  26. HttpGet get = new HttpGet(uriBuilder.build());
  27. CloseableHttpResponse response = httpClient.execute(get);
  28. int statusCode = response.getStatusLine().getStatusCode();
  29. System.out.println(statusCode);
  30. HttpEntity entity = response.getEntity();
  31. String string = EntityUtils.toString(entity, "utf-8");
  32. System.out.println(string);
  33. response.close();
  34. httpClient.close();
  35. }
  36. @Test
  37. public void doPost() throws Exception {
  38. CloseableHttpClient httpClient = HttpClients.createDefault();
  39. //创建一个post对象
  40. HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.action");
  41. //执行post请求
  42. CloseableHttpResponse response = httpClient.execute(post);
  43. String string = EntityUtils.toString(response.getEntity());
  44. System.out.println(string);
  45. response.close();
  46. httpClient.close();
  47. }
  48. @Test
  49. public void doPostWithParam() throws Exception{
  50. CloseableHttpClient httpClient = HttpClients.createDefault();
  51. //创建一个post对象
  52. HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.action");
  53. //创建一个Entity。模拟一个表单
  54. List<NameValuePair> kvList = new ArrayList<>();
  55. kvList.add(new BasicNameValuePair("username", "张三"));
  56. kvList.add(new BasicNameValuePair("password", "123"));
  57. //包装成一个Entity对象
  58. StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8");
  59. //设置请求的内容
  60. post.setEntity(entity);
  61. //执行post请求
  62. CloseableHttpResponse response = httpClient.execute(post);
  63. String string = EntityUtils.toString(response.getEntity());
  64. System.out.println(string);
  65. response.close();
  66. httpClient.close();
  67. }
  68. }

 封装工具类

  1. public class HttpClientUtil {
  2. public static String doGet(String url, Map<String, String> param) {
  3. // 创建Httpclient对象
  4. CloseableHttpClient httpclient = HttpClients.createDefault();
  5. String resultString = "";
  6. CloseableHttpResponse response = null;
  7. try {
  8. // 创建uri
  9. URIBuilder builder = new URIBuilder(url);
  10. if (param != null) {
  11. for (String key : param.keySet()) {
  12. builder.addParameter(key, param.get(key));
  13. }
  14. }
  15. URI uri = builder.build();
  16. // 创建http GET请求
  17. HttpGet httpGet = new HttpGet(uri);
  18. // 执行请求
  19. response = httpclient.execute(httpGet);
  20. // 判断返回状态是否为200
  21. if (response.getStatusLine().getStatusCode() == 200) {
  22. resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
  23. }
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. } finally {
  27. try {
  28. if (response != null) {
  29. response.close();
  30. }
  31. httpclient.close();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. return resultString;
  37. }
  38. public static String doGet(String url) {
  39. return doGet(url, null);
  40. }
  41. public static String doPost(String url, Map<String, String> param) {
  42. // 创建Httpclient对象
  43. CloseableHttpClient httpClient = HttpClients.createDefault();
  44. CloseableHttpResponse response = null;
  45. String resultString = "";
  46. try {
  47. // 创建Http Post请求
  48. HttpPost httpPost = new HttpPost(url);
  49. // 创建参数列表
  50. if (param != null) {
  51. List<NameValuePair> paramList = new ArrayList<>();
  52. for (String key : param.keySet()) {
  53. paramList.add(new BasicNameValuePair(key, param.get(key)));
  54. }
  55. // 模拟表单
  56. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
  57. httpPost.setEntity(entity);
  58. }
  59. // 执行http请求
  60. response = httpClient.execute(httpPost);
  61. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. } finally {
  65. try {
  66. response.close();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. return resultString;
  72. }
  73. public static String doPost(String url) {
  74. return doPost(url, null);
  75. }
  76. public static String doPostJson(String url, String json) {
  77. // 创建Httpclient对象
  78. CloseableHttpClient httpClient = HttpClients.createDefault();
  79. CloseableHttpResponse response = null;
  80. String resultString = "";
  81. try {
  82. // 创建Http Post请求
  83. HttpPost httpPost = new HttpPost(url);
  84. // 创建请求内容
  85. StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
  86. httpPost.setEntity(entity);
  87. // 执行http请求
  88. response = httpClient.execute(httpPost);
  89. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. } finally {
  93. try {
  94. response.close();
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. return resultString;
  100. }
  101. }

HttpClientUtil

httpclient介绍与使用的更多相关文章

  1. HttpClient介绍和使用

    HttpClient介绍和使用 今天有一个需求:后台访问一个接口,获取返回的数据.于是找到了HttpClient 1.介绍 SpringCloud中服务和服务之间的调用全部是使用HttpClient, ...

  2. 接口测试——httpclient介绍与请求方式详解

    httpClient工具介绍 HTTP协议可能是现在lntemet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源.虽然在JDK的java.net包中已经 ...

  3. httpclient介绍与请求方式详解

    httpClient工具介绍 HTTP协议可能是现在lntemet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源.虽然在JDK的java.net包中已经 ...

  4. httpclient介绍

    前言 超文本传输协议(HTTP)也许是当今互联网上使用的最重要的协议了.Web服务,有网络功能的设备和网络计算的发展,都持续扩展了HTTP协议的角色,超越了用户使用的Web浏览器范畴,同时,也增加了需 ...

  5. HttpClient介绍和简单使用流程

    HttpClient SpringCloud中服务和服务之间的调用全部是使用HttpClient,还有前面使用SolrJ中就封装了HttpClient,在调用SolrTemplate的saveBean ...

  6. 【JAVA】通过HttpClient发送HTTP请求的方法

    HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...

  7. 理解并使用.NET 4.5中的HttpClient

    HttpClient介绍 HttpClient是.NET4.5引入的一个HTTP客户端库,其命名空间为System.Net.Http..NET 4.5之前我们可能使用WebClient和HttpWeb ...

  8. Android入门:用HttpClient模拟HTTP的GET和POST请求

    一.HttpClient介绍   HttpClient是用来模拟HTTP请求的,其实实质就是把HTTP请求模拟后发给Web服务器:   Android已经集成了HttpClient,因此可以直接使用: ...

  9. HttpClient中异步方法的同步调用

    在System.Net.Http中,提供了使用Http与远程服务器通讯的httpClient,但是里面都是异步方法,有时候我们并不需要使用异步操作.这个时候可以使用如下的方式来进行同步调用. clas ...

随机推荐

  1. 微信小程序点击图片放大预览

    微信小程序点击图片放大预览使用到 wx.previewImage 接口,可以放大.上/下一张 上代码 wxml代码 <view class='content-img' wx:if="{ ...

  2. PHP 绘制验证码

    使用PHP绘制验证码  可直接使用 // 验证码 <?php $checkCode = ""; for ($i=0; $i < 4; $i++) { // 十进制转换为 ...

  3. ubuntu16.4+nginx+uwsgi+Django 部署上线

    Nginx概述 Nginx是一款轻量级的HTTP服务器,采用事件驱动和异步非阻塞处理方式框架,这让其具有极好的IO性能,市场用于服务端的反向代理和负载均衡 Nginx优点 高并发连接:官方测试Ngin ...

  4. Android工程中javax annotation Nullable找不到的替代方案

    我们在某些Android开源库中会遇到下面的引用找不到的问题:import javax.annotation.Nonnull;import javax.annotation.Nullable; 其实A ...

  5. JS截取字符串方法实例

    // JS截取字符串可使用 substring()或者slice() // // 函数:substring() // 定义:substring(start,end)表示从start到end之间的字符串 ...

  6. 韩顺平php从入门到精通

    37 整型细节说明 $a; echo $a; var_dump($a) //NULL 一个数总是要占用内存空间(字节),在php中一个整数一般占用四个字节(与平台相关),一个字节占用8bit php的 ...

  7. CSS3新特性,兼容性,兼容方法总结

    css3手册css3手册 边框 border-radius 用于添加圆角效果 语法: border-radius:[ <length> | <percentage> ]{1,4 ...

  8. VS插件VisualSVN v5.2.3.0 破解文件

    分享一个VisualSVN v5.2.3的破解文件: >>>> 点此下载 <<<< 下载后,找到VisualSVN的安装目录,例如:C:\Program ...

  9. HttpWebRequest抓取网页内容与直接输入URL得到的内容不一致!球大神帮忙!!

    一.前言 我在做一个百度收录情况查询的软件,就是通过软件来批量查询文章链接是否被百度收录,主要是用来查询某个网址的收录次数还有网站的排行数,思路是借鉴别人的. 二.问题描述 首先需要考虑的是能够支持哪 ...

  10. Windows 7 任务栏图标消失(变透明,仍然占有地方,但是点击无反应)的解决方法

    解决方案:清理资源管理器缓存(重启资源管理器) 1.打开程序管理器(ctrl+shift+esc) 2.在进程那里找到"explorer.exe",然后按结束进程 3.然后在文件( ...