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. js if(!!!e) {} 判断条件中的三个感叹号什么意思

    两个感叹号的意思就是,将变量转换为其对应的布尔值. !!e就是e对应的布尔值,true或者false. !!!e==!(!!e)==!true/!false=false/true;

  2. springboot基于方法级别注解事务的多数据源切换问题

    springBoot多数据源配置 配置读数据源 @Component @ConfigurationProperties(prefix = "jdbc.read") @Propert ...

  3. XSS-笔记

     Cross Site Script  跨站脚本 是一种客户端代码的注入  而命令注入.sql注入都是客户端代码的注入.   XSS攻击行为的目标为:1.窃取目标的cookie信息 2.执行CSRF脚 ...

  4. springboot2.0处理自定义异常始终返回json

    1. 编写自定义异常类 package cn.jfjb.crud.exception; /** * @author john * @date 2019/11/24 - 9:48 */ public c ...

  5. javaweb:Response/Request的概述 (转发、重定向、get/post)转

    请求响应流程图 1]response 1   response概述 response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServletR ...

  6. [.net core]8.中间件的概念

    假设我们的中间件是这样的(可以自由排列, 扩展自定义中间件) logging负责记录请求/响应 staticFiles 负责响应 静态文件 MVC 负责响应 视图 当.net core web app ...

  7. synchronize和lock的区别 & synchionzie与volatile的区别

    synchronized与Lock的区别 https://www.cnblogs.com/iyyy/p/7993788.html Lock和synchronized和volatile的区别和使用 ht ...

  8. static修饰的成员与非static修饰类的成员的区别

    ① 格式 : 1> static修饰的,称为静态成员,非static修饰的,称为非静态成员. ② 内存位置: 1>static修饰的,在方法区的静态区中,非static修饰的,在堆中的对象 ...

  9. luogu P4654 [CEOI2017]Mousetrap

    传送门 这里把终点设为根方便后续处理,那么目标就是要让老鼠走到根 首先考虑老鼠动不了的情况,这种情况下可以把从这个点到终点路径上的分支堵住,然后再疏通路径上的走过的边,可以发现这是这种情况下最优的决策 ...

  10. 史上最简单JS复制功能,兼容安卓ios!

    1.JS复制原理: 被复制内容的元素不能被其他元素遮盖,否则无效.  (设置opacity透明为0,不可以设置display:none); 2.常规的复制方法 function copyUrl2() ...