当前位置:   article > 正文

HttpURLConnection中请求头中携带Token的使用方法_http将token塞到请求头

http将token塞到请求头
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class TokenRequestExample {

    public static void main(String[] args) throws IOException {
        // 设置API地址
        String apiUrl = "
        
        // 设置Token
        String token = "your_token_here";
        // 创建URL对象
        URL url = new URL(apiUrl);
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // 设置请求头中的Authorization字段
        connection.setRequestProperty("Authorization", "Bearer " + token);
        
        // 发送GET请求
        connection.setRequestMethod("GET");
        // 获取响应结果
        int responseCode = connection.getResponseCode();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // 处理响应结果
        if (responseCode == HttpURLConnection.HTTP_OK) {
            System.out.println("请求成功!");
            System.out.println("响应内容:" + response.toString());
        } else {
            System.out.println("请求失败!");
            System.out.println("响应代码:" + responseCode);
            System.out.println("响应内容:" + response.toString());
        }

        // 关闭连接
        connection.disconnect();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

import java.net.HttpURLConnection;

/**
 * HttpGet请求
 * @param vurl:请求地址,map:{头部信息}
 * @return 返回消息
 */
public static String httpGet(String vurl,HashMap<String, Object> map) {
    try {
        URL url = new URL(vurl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        for (Map.Entry item : map.entrySet()) {
            connection.setRequestProperty(item.getKey().toString(),item.getValue().toString());//设置header
        }
        InputStream in = connection.getInputStream();
        InputStreamReader isr = new InputStreamReader(in, "utf-8");
        BufferedReader br = new BufferedReader(isr);
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        isr.close();
        in.close();
        return sb.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

一般会在头部添加认证信息,如token值或BasicAuth认证的 Authorization值

HashMap<String, Object> tmap = new HashMap<String, Object>();
tmap.put("Authorization",authorization);//tmap.put("token","tonken值");
String vmsg= Comm.httpGet(vurl,tmap);//获取请求的返回结果
  • 1
  • 2
  • 3
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/137883
推荐阅读
相关标签
  

闽ICP备14008679号