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相关推荐

  1. Spring boot RestTemplate 统一添加 Header

    RestTemplate 统一添加 Header 一.添加拦截器 public class HeaderRequestInterceptor implements ClientHttpRequestI ...

  2. Android RecyclerView添加Header头部

     Android RecyclerView添加Header头部 Android RecyclerView不像以前的ListView那样直接添加头部,如果要给RecyclerView增加头部,则需要 ...

  3. Jmeter(一)http接口添加header和cookie --转载

    Jmeter(一)http接口添加header和cookie HTTP信息头管理器在Jmeter的使用过程中起着很重要的作用,通常我们在通过Jmeter向服务器发送http请求(get或者post)的 ...

  4. RecyclerView添加header与footer

    前言 这次主要关于RecyclerView添加header和footer的实现方法,我们都知道,在使用ListView的时候我们能自由的给自己的ListView添加头部与尾部.使用addHeaderV ...

  5. gridview添加header

    gridview是不能添加header的,这里的解决方法是将listview改造成gridview使用,功能很好用,唯一的缺点是列数不能自适应 示例代码下载地址http://pan.baidu.com ...

  6. Android添加Header请求参数实例,java响应header请求实现demo

    1.首先添加AsyncHttpClient.jar包到libs文件夹下 2.初始化请求类以及响应回调类 private AsyncHttpClient client; private AsyncHtt ...

  7. Jmeter(一)http接口添加header和cookie

    HTTP信息头管理器在Jmeter的使用过程中起着很重要的作用,通常我们在通过Jmeter向服务器发送http请求(get或者post)的时候,往往后端需要一些验证信息,比如说web服务器需要带过去c ...

  8. android recyclerview添加头部,Android RecyclerView添加Header头部

     Android RecyclerView添加Header头部 Android RecyclerView不像以前的ListView那样直接添加头部,如果要给RecyclerView增加头部,则需要 ...

  9. swagger ui 怎么输入对象_java swagger ui 添加header请求头参数的方法

    我用到的swagger 主要有三款产品,swagger editor,swagger ui 和swagger codegen. swagger editor:主要是一个本地客户端,用来自己添加api, ...

最新文章

  1. 移动互联网用户的心理需求【转载】
  2. 专家:“十三五”中国应建立覆盖城乡的超级WIFI
  3. python casefold lower_Python学习之路(2)——字符串方法casefold和lower的区别(Python3.5)-Go语言中文社区...
  4. unsigned short占几个字节_ARM中,字、半字、字节分别是多少位?
  5. 什么是jsf_为什么应该避免JSF
  6. 如何用c语言程序写一段英文字母,菜鸟求助,写一个随机输出26个英文字母的程序...
  7. ftp服务器生成文件,ftp服务器自动生成文件
  8. Spark内存空间分配机制
  9. 处理非window设置为window的Owner
  10. 网络编程资源大集合(包含前端、java、linux、安卓、github开源项目、开发工具等)
  11. 使用vba宏/python代码更新word目录——只更新页码
  12. 【大前端】用html和css写一个QQ邮箱登录页面
  13. sourceForge, wikipedia与异形
  14. sentry mysql_Apache Sentry安装及简单使用
  15. 2019校招真题编程(十九)魔法深渊
  16. 国外计算机专业用windows,最受欢迎的四款国外流行的Win7系统优化软件
  17. 旅行社软件测试培训,旅行社管理系统软件测试总结分析方案(65页)-原创力文档...
  18. Python正则表达式模式
  19. 一套完整的动环监控系统,适用于各类机房、学校机房、医院机房、银行库房等
  20. ZLib 解压缩工具

热门文章

  1. 学习前端需要知道的英语单词(部分)
  2. 自学网络安全|什么是网络安全
  3. 黄聪mysql_黄聪:mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法
  4. ECharts 顶部 label 换行
  5. openstack是什么?openstack的主要功能组件有哪些
  6. python全栈开发—函数整理(最后一次整理,不能再全了)
  7. [MFC]MFC实现UDP客户端和服务端信息交互
  8. Kubernetes 名字由来
  9. 阿里云服务器使用+OSS的使用
  10. 英文论文中的abstract如何写?