1、httpClient的使用

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
// 第一步:创建一个httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault(); // 第二步:创建一个HttpPost对象。需要指定一个url
// HttpPost post = new HttpPost("http://47.244.48.xxx/xxx/xxx/");
HttpPost post = new HttpPost("http://127.0.0.1:8080/xxx/xxx/"); // 携带cookie
String sessionId = "PHPSESSID=KSoxfJlB20JJ0...";
BasicHeader header = new BasicHeader("Cookie", sessionId);
post.setHeader(header); // 第三步:创建一个list模拟表单,list中每个元素是一个NameValuePair对象
List<NameValuePair> formList = new ArrayList<>();
formList.add(new BasicNameValuePair("xxx", "xxx"));
formList.add(new BasicNameValuePair("xxx", "xxx")); // 第四步:需要把表单数据包装到Entity对象中: StringEntity
StringEntity entity = new UrlEncodedFormEntity(formList, "utf-8");
post.setEntity(entity); // 第五步:执行请求。
CloseableHttpResponse response = httpClient.execute(post); // 第六步:接收返回结果
HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString(httpEntity);
System.out.println(result); // 第七步:关闭流。
response.close();
httpClient.close();

2、RestTemplate的使用:get请求带Cookie

String url = Config.XXX_BASE_URL + "/userlevel?inviterId=" + invit1;
String restInfo = MessageFormat.format("url:{0}, inviterId:{1}", url, invit1);
UtilFunctions.log.info("UserController#register call rest api info, " + restInfo); HttpHeaders requestHeaders = new HttpHeaders();
List<String> cookieList = UtilFunctions.getCookieList(request);
requestHeaders.put("Cookie", cookieList);
HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
restTemplate.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class).getBody();

  

  UtilFunctions类的getCookieList()方法

public static List<String> getCookieList(HttpServletRequest request) {
List<String> cookieList = new ArrayList<>(); Cookie[] cookies = request.getCookies();
if (cookies == null || cookies.length == 0) {
return cookieList;
} for (Cookie cookie : cookies) {
cookieList.add(cookie.getName() + "=" + cookie.getValue());
} return cookieList;
}

参考资料:

  1)Springboot — 用更优雅的方式发HTTP请求(RestTemplate详解)

httpClient和RestTemplate的使用的更多相关文章

  1. webservice的接口协议(HTTPClient 、RestTemplate HttpURLConnection)

    HTTP协议时Internet上使用的很多也很重要的一个协议,越来越多的java应用程序需要通过HTTP协议来访问网络资源. HTTPClient提供的主要功能: 1.实现了所有HTTP的方法(GET ...

  2. Httpclient与RestTemplate的比较(比httpClient更优雅的Restful URL访问)

    一.HttpClient (一)HttpClient 客户端 1.HttpClient 是 apache 的开源,需要引入两个包:httpclient-4.2.4.jar 和 httpcore-4.2 ...

  3. HttpClient&&RestTemplate学习

    1. 什么是HttpClient HttpClient是Apache下面的子项目,可以提供高效的,最新的,功能丰富的支持HTTP协议的客户端编程工具包. 2. 为什么要学习HttpClient Htt ...

  4. RestTemplate发送HTTP、HTTPS请求

    RestTemplate 使用总结   场景: 认证服务器需要有个 http client 把前端发来的请求转发到 backend service, 然后把 backend service 的结果再返 ...

  5. Springboot+RestTemplate 简单使用

        spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值 ...

  6. SpringBoot中使用RestTemplate

    spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值类型即可 ...

  7. 写的一个HttpClient类

    package com.ca.test.cainterface.common.util.http; import com.ca.test.cainterface.common.util.data.Da ...

  8. RestTemplate使用教程

    原文地址:https://www.cnblogs.com/f-anything/p/10084215.html 一.概述 spring框架提供的RestTemplate类可用于在应用中调用rest服务 ...

  9. RestTemplate实践(及遇到的问题)

    在微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Apache的Http Client.Netty的异步 ...

随机推荐

  1. 【FFMPEG】网络流媒体协议

    目录(?)[-] RTP RTCP SRTP SRTCP RTSP RTSP 和RTP的关系 SDP RTMPRTMPS mms HLS RTP           参考文档 RFC3550/RFC3 ...

  2. adb将Apk内置到系统中(system/priv-app)

    https://blog.csdn.net/starhosea/article/details/78697007 so文件的处理是目前遇到过的问题.文章中解释了. 正文: 有时候我们在Android ...

  3. [ASP.NET] 数据绑定以及Container.DataItem的具体分析 [转]

    灵活的运用数据绑定操作        绑定到简单属性:<%#UserName%>        绑定到集合:<asp:ListBox id="ListBox1" ...

  4. 【转】MySQL-Utilities,mysql工具包

    原文:https://blog.csdn.net/leshami/article/details/52795777 MySQL Utilities 是一组基于python语言编写的python库的命令 ...

  5. C++ Primer: 1. 初识输入和输出

    C++没有定义任何的输入和输出语句,而是使用了 标准库来提供IO机制---iostream; 标准库iostream定义了4种不同的IO对象: cin:     标准输入对象:instream类型的对 ...

  6. 用C#取个中文名字

    *注意:此方法获得的名字很可能出现生僻字,若要get一个好记/常见的名字,还请另作操作. 以百家姓(444个单姓,60个复姓)作为姓氏,再添加两个随机的中文,You can get a chinese ...

  7. chrome://inspect 白屏BUG解决方案

    chrome://inspect 是前端常用的调试工具但是有时候会碰到PC端dev-tools白屏的问题 1.首先我们要处于[科][学]的网络环境 2.在 chrome 中输入chrome://app ...

  8. Layui关闭弹出层并刷新父窗口

    先确保已经引入layui和jquery 再确保初始化layer弹出层 <script> layui.use(['form', 'layer'], function() { var form ...

  9. php 数组助手类

    ArrayHelper.php <?php /** * php 数组助手类 * Class ArrayHelper * @package app\helper */ class ArrayHel ...

  10. 图数据库:AgensGraph

    文章目录 AgensGraph简介 官网及下载 安装AgensGraph 上传并解压 添加agens用户 配置.bashrc 初始化并启动 初始化数据库 启动数据库 执行交互式终端 图数据库基础概念 ...