当前位置: 首页 > article >正文

发送get请求并且发送请求头(header),java实现

发送get请求时,发送请求头(Header)中的内容

方便第二次调用其他url时传递参数,例如userCode或者租户编码

调用方式

@Autowired
private HttpServletRequest request;

先注入HttpServletRequest


public xxx xxx(){
    String url = "http://" + ip +":8082/inAndOut/into/xxxxxx";
    String userCode = request.getHeader("usercode");

    //动态传递Header中的userCode,用来给组件服务接口传userCode
    Map<String, String> headers = new HashMap<>();
    headers.put("Content-Type", "application/json");
    headers.put("Authorization", "Bearer your_token_here");
    headers.put("tenantcode", tenantCode);
    headers.put("userCode", userCode);
    String request = HttpClientUtil.sendGetWithHeaders(url,headers);
}

userCode和tenantCode可以从Header中获取,前端也一样,这是在PostMan中测试

工具类

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class GetWithHeadersExample {

    public static void main(String[] args) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Authorization", "Bearer your_token_here");

        String url = "http://example.com/api/endpoint";

        try {
            String response = sendGetWithHeaders(url, headers);
            System.out.println("Response: " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String sendGetWithHeaders(String url, Map<String, String> headers) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);

        for (Map.Entry<String, String> entry : headers.entrySet()) {
            httpGet.addHeader(entry.getKey(), entry.getValue());
        }

        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();

        String responseBody = EntityUtils.toString(entity);
        httpClient.close();

        return responseBody;
    }
}


http://www.kler.cn/news/234191.html

相关文章:

  • linux系统非关系型数据库redis
  • re:从0开始的CSS学习之路 10. 盒子模型的溢出
  • Python OCR 之旅:PaddleOCR 与 pytesseract 比较及应用
  • 02 动力云客之登陆界面
  • MySQL 的UI
  • vue+springboot前后端视频文件等的上传与展示(基于七牛云)
  • c语言的各类输出函数(带完善更新)
  • 数据分析基础之《pandas(6)—高级处理》
  • uniapp 本地存储的方式
  • 156基于Matlab的光纤陀螺随机噪声和信号
  • 「优选算法刷题」:数青蛙
  • 如何系统的自学Python?通义千问、讯飞星火、文心一言及ChatGPT的回答
  • 复习面经哦
  • effective c++ 笔记 条款13-18
  • 飞天使-k8s知识点14-kubernetes散装知识点3-Service与Ingress服务发现控制器
  • Python中使用multiprocessing模块创建进程
  • MYSQL笔记:约束条件
  • 算法||实现典型数据结构的查找、添加和删除数据 并分析其时间和空间复杂度
  • 最佳视频转换器软件:2024年视频格式转换的选择
  • React Emotion 如何优雅的使用样式(一)
  • 人物系统构建1
  • 使用raw.gitmirror.com替换raw.githubusercontent.com以解决brew upgrade python@3.12慢的问题
  • 问题:2、计算机网络的目标是实现________。 #媒体#知识分享
  • 第十六章 以编程方式使用 SQL 网关 - %SQLGatewayConnection 方法和属性
  • 知识图谱与图神经网络融合:构建智能应用的新前沿
  • [145] 二叉树的后序遍历 js
  • /etc/apt/sources.list 包含ubuntu18.04或bionic字样的解决思路
  • C语言字符常量与字符变量..
  • 前端修炼手册(uniapp的api篇)
  • Ansys方法基础