restTemplate添加header
1、httpClient
public static void main(String[] args) throws Exception {HashMap<String, String> hashMap = new HashMap<>();hashMap.put("key", "value");String body = JSONObject.toJSONString(hashMap);post(body);}/*** 发送 post请求*/public static void post(String body) throws Exception {// 获得Http客户端CloseableHttpClient httpclient = HttpClientBuilder.create().build();// 创建httpPostHttpPost httppost = new HttpPost("替换连接地址");//设置参数体httppost.setHeader("Content-Type", "application/json;charset=UTF-8");//装填参数StringEntity s = new StringEntity(body, "utf-8");//设置参数到请求对象中httppost.setEntity(s);CloseableHttpResponse response = httpclient.execute(httppost);try {HttpEntity entity = response.getEntity();if (entity != null) {System.out.println("--------------------------------------");System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));System.out.println("--------------------------------------");}} finally {response.close();}try {httpclient.close();} catch (IOException e) {e.printStackTrace();}}
2、restTemplate
header可以这样添加
public static void main(String[] args) throws Exception {String url = "替换连接地址";// 请求体HashMap<String, String> hashMap = new HashMap<>();hashMap.put("key", "value");String body = JSONObject.toJSONString(hashMap);// 请求头HttpHeaders headers = new HttpHeaders();headers.add("Content-Type", "application/json;charset=UTF-8");// 请求HttpEntity<String> request = new HttpEntity<>(body, headers);// 使用RestTemplate请求RestTemplate restTemplateHttps = new RestTemplate();ResponseEntity<JSONObject> responseBody = restTemplateHttps.postForEntity(url, request, JSONObject.class);JSONObject httpBody = responseBody.getBody();System.out.println("接口返回参数:" + httpBody);}
restTemplate添加header相关推荐
- Spring boot RestTemplate 统一添加 Header
RestTemplate 统一添加 Header 一.添加拦截器 public class HeaderRequestInterceptor implements ClientHttpRequestI ...
- Android RecyclerView添加Header头部
Android RecyclerView添加Header头部 Android RecyclerView不像以前的ListView那样直接添加头部,如果要给RecyclerView增加头部,则需要 ...
- Jmeter(一)http接口添加header和cookie --转载
Jmeter(一)http接口添加header和cookie HTTP信息头管理器在Jmeter的使用过程中起着很重要的作用,通常我们在通过Jmeter向服务器发送http请求(get或者post)的 ...
- RecyclerView添加header与footer
前言 这次主要关于RecyclerView添加header和footer的实现方法,我们都知道,在使用ListView的时候我们能自由的给自己的ListView添加头部与尾部.使用addHeaderV ...
- gridview添加header
gridview是不能添加header的,这里的解决方法是将listview改造成gridview使用,功能很好用,唯一的缺点是列数不能自适应 示例代码下载地址http://pan.baidu.com ...
- Android添加Header请求参数实例,java响应header请求实现demo
1.首先添加AsyncHttpClient.jar包到libs文件夹下 2.初始化请求类以及响应回调类 private AsyncHttpClient client; private AsyncHtt ...
- Jmeter(一)http接口添加header和cookie
HTTP信息头管理器在Jmeter的使用过程中起着很重要的作用,通常我们在通过Jmeter向服务器发送http请求(get或者post)的时候,往往后端需要一些验证信息,比如说web服务器需要带过去c ...
- android recyclerview添加头部,Android RecyclerView添加Header头部
Android RecyclerView添加Header头部 Android RecyclerView不像以前的ListView那样直接添加头部,如果要给RecyclerView增加头部,则需要 ...
- swagger ui 怎么输入对象_java swagger ui 添加header请求头参数的方法
我用到的swagger 主要有三款产品,swagger editor,swagger ui 和swagger codegen. swagger editor:主要是一个本地客户端,用来自己添加api, ...
最新文章
- 移动互联网用户的心理需求【转载】
- 专家:“十三五”中国应建立覆盖城乡的超级WIFI
- python casefold lower_Python学习之路(2)——字符串方法casefold和lower的区别(Python3.5)-Go语言中文社区...
- unsigned short占几个字节_ARM中,字、半字、字节分别是多少位?
- 什么是jsf_为什么应该避免JSF
- 如何用c语言程序写一段英文字母,菜鸟求助,写一个随机输出26个英文字母的程序...
- ftp服务器生成文件,ftp服务器自动生成文件
- Spark内存空间分配机制
- 处理非window设置为window的Owner
- 网络编程资源大集合(包含前端、java、linux、安卓、github开源项目、开发工具等)
- 使用vba宏/python代码更新word目录——只更新页码
- 【大前端】用html和css写一个QQ邮箱登录页面
- sourceForge, wikipedia与异形
- sentry mysql_Apache Sentry安装及简单使用
- 2019校招真题编程(十九)魔法深渊
- 国外计算机专业用windows,最受欢迎的四款国外流行的Win7系统优化软件
- 旅行社软件测试培训,旅行社管理系统软件测试总结分析方案(65页)-原创力文档...
- Python正则表达式模式
- 一套完整的动环监控系统,适用于各类机房、学校机房、医院机房、银行库房等
- ZLib 解压缩工具