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

发送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;
    }
}

相关文章:

  • macbook电脑如何永久删除app软件?
  • 开发JSP应用程序
  • Unity3d Shader篇(五)— Phong片元高光反射着色器
  • 【回溯算法】 LCR 081. 组合总和
  • android 音频调试技巧
  • 米哈游(原神)终面算法原题
  • Flink从入门到实践(一):Flink入门、Flink部署
  • C++实现鼠标点击和获取鼠标位置(编译环境visual studio 2022)
  • SQL 表信息 | 统计 | 脚本
  • 【笔记】Helm-5 Chart模板指南-8 命名模板
  • QT学习(五)C++函数重载
  • SpringCloud--Eureka注册中心服务搭建注册以及服务发现
  • 基于OpenCV灰度图像转GCode的斜向扫描实现
  • Unity UGUI实现点击事件穿透
  • C++面试宝典第27题:完全平方数之和
  • 基于 Java 的小说阅读器小程序,附源码
  • PVST详解
  • CSS 闪电按钮效果
  • 【python】绘制春节烟花
  • Vagrant 虚拟机工具基本操作指南
  • 河南博物院:警惕非官方网络平台 “买讲解保进馆” 等虚假信息
  • 三亚回应“游客骑摩托艇出海遇暴雨”:未失联,已引导申请先行赔付
  • 西湖大学2025年上海市综合评价招生简章发布
  • 耶路撒冷发生山火,以防长宣布紧急状态
  • 两部门调度部署“五一”假期安全防范工作,要求抓好旅游安全
  • 上海国际咖啡文化节开幕,北外滩集结了超350个展位