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. DHCP迁移

    情况1:windows 2003迁移到windows 2003或者windows 2008,按照需要以下几个步骤:1.在源DHCP服务器导出DHCP数据文件,执行以下命令netsh dhcp serv ...

  2. python基础及安装

    一.python介绍 介绍  python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,Guido开始写能够解释Python语言语法的解释器.Python这个名 ...

  3. CentOS下firewalld添加开放端口

    添加 firewall-cmd --zone=public --add-port=/tcp --permanent (--permanent永久生效,没有此参数重启后失效) 重新载入 firewall ...

  4. CF140C New Year Snowmen(贪心+优先队列)

    CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...

  5. priority_queue的常见用法

    priority_queue的常见用法 priority_queue是什么? 优先队列 底层实现用堆来实现 每次队首的优先级最大 priority_queue的定义 引入头文件 # include & ...

  6. PHP常用代码片段

    /** * 高效判断远程文件是否存在 * @param $file * @return bool 存在返回 true 不存在或者其他原因返回false */ function remoteFileEx ...

  7. 最近用到的postgresql 的一些操作

    通过Linux命令行连接:在本机上连接操作 第一步:su - postgres进入到这个:-bash-4.2$ 第二步:psql进入到数据库 切换数据库: 命令:\c dbname 进入到数据库中 查 ...

  8. QtSpim使用Tips

    QtSpim使用记录 垃圾QtSpim,输入中文会死机 MIPS的中文资料奇缺,如果有问题建议google参考英文资料,许多外国大学的网站上有对MIPS各方面的详细介绍 QtSpim是MIPS处理器的 ...

  9. MySQL数据库主从同步实战过程

       Linux系统MySQL数据库主从同步实战过程 安装环境说明 系统环境: [root@~]# cat /etc/redhat-release CentOS release 6.5 (Final) ...

  10. vue elementui table组件内容换行

    解决方案 tableData = [ { "name": "domain111", "metric": [ "平均耗时" ...