httpClient和RestTemplate的使用
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的使用的更多相关文章
- webservice的接口协议(HTTPClient 、RestTemplate HttpURLConnection)
HTTP协议时Internet上使用的很多也很重要的一个协议,越来越多的java应用程序需要通过HTTP协议来访问网络资源. HTTPClient提供的主要功能: 1.实现了所有HTTP的方法(GET ...
- Httpclient与RestTemplate的比较(比httpClient更优雅的Restful URL访问)
一.HttpClient (一)HttpClient 客户端 1.HttpClient 是 apache 的开源,需要引入两个包:httpclient-4.2.4.jar 和 httpcore-4.2 ...
- HttpClient&&RestTemplate学习
1. 什么是HttpClient HttpClient是Apache下面的子项目,可以提供高效的,最新的,功能丰富的支持HTTP协议的客户端编程工具包. 2. 为什么要学习HttpClient Htt ...
- RestTemplate发送HTTP、HTTPS请求
RestTemplate 使用总结 场景: 认证服务器需要有个 http client 把前端发来的请求转发到 backend service, 然后把 backend service 的结果再返 ...
- Springboot+RestTemplate 简单使用
spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值 ...
- SpringBoot中使用RestTemplate
spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值类型即可 ...
- 写的一个HttpClient类
package com.ca.test.cainterface.common.util.http; import com.ca.test.cainterface.common.util.data.Da ...
- RestTemplate使用教程
原文地址:https://www.cnblogs.com/f-anything/p/10084215.html 一.概述 spring框架提供的RestTemplate类可用于在应用中调用rest服务 ...
- RestTemplate实践(及遇到的问题)
在微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Apache的Http Client.Netty的异步 ...
随机推荐
- 10大IT社区
技术社区导航 http://tooool.org/ 1. cnblogs 人多内容质量最高 2.csdn csdn的注册人数多,但新手多 3.java eye java eye注册用户刚突破10万,但 ...
- hue的load balance
参考: hue的load balance官网: https://www.cloudera.com/documentation/enterprise/6/6.2/topics/hue_use_add_l ...
- etcd集群移除节点
查看当前集群信息 # etcdctl member list --write-out=table +------------------+---------+--------------------+ ...
- [转帖]数据库默认驱动、URL、端口
超详细的各种数据库默认驱动.URL.端口总结 http://database.51cto.com/art/201906/598043.htm 学习了解一下. 概述 今天主要对各种数据库默认端口和UR ...
- day 02 while 循环 格式化输出 运算符 and or not - 编码的初识
while 循环 while 条件: 循环体 循环如何终止? 改变条件. flag = Truewhile flag: print('狼的诱惑') print('我们不一样') ...
- javascript异步延时加载及判断是否已加载js/css文件
<html> <head> <script type="text/javascript"> /**======================= ...
- 启动angular项目,端口被占用
启动项目,npm start,报如下错误: 解决:1.win+R,输入CMD,启动命令行窗口. 2.输入netstat -ano或者netstat -ano|findstr 8080,查看8080端口 ...
- N4复习考试总结
一つ(ひとつ) 半分(はんぶん) 煙草(たばこ)を吸う(すう) 玄関(げんかん) ナイフ(刀) 財布(さいふ) 浅い(あさい) 薄い(うすい) 牛乳(ぎゅうにゅう) 皿(さら) 七日(なのか) ...
- ubuntu18.04 LTS上安装并使用nvm管理node版本
1. aaa nvm是一个非常不错的node版本管理器,类似于ruby的rvm. 其github地址为https://github.com/creationix/nvm. 此处介绍一下如何在ubunt ...
- python语音提示
#coding:utf8 import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice") whi ...