java获取http请求的Header和Body

在开发过程中,我们请求后台接口时,有时需要携带一些必要的参数信息,但是把它放在url中又有点不太合适,我们往往可以把参数信息放在请求头的body中进行请求即可。

在http请求中,有Header和Body之分,读取header使用request.getHeader("…");

读取Body使用request.getReader(),但getReader获取的是BufferedReader,需要把它转换成字符串,下面是转换的方法

public class TestController {

    @RequestMapping("/a")
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response, BufferedReader br)
            throws ServletException, IOException {//Header部分
        System.out.print(request.getHeaderNames());
        Enumeration<?> enum1 = request.getHeaderNames();
        while (enum1.hasMoreElements()) {
            String key = (String) enum1.nextElement();
            String value = request.getHeader(key);
            System.out.println(key + "\t" + value);
        }
//body部分
        String inputLine;
        String str = "";
        try {
            while ((inputLine = br.readLine()) != null) {
                str += inputLine;
            }
            br.close();
        } catch (IOException e) {
            System.out.println("IOException: " + e);
        }
        System.out.println("str:" + str);
    }
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 Java 中的 HttpURLConnection 类来发送 HTTPS 请求,并在请求中添加 Header。 下面是一个示例代码,其中我们使用 Base64 编码来将账号和密码组成的字符串转换为 Authorization Header 中的值。 ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Base64; public class HttpsRequestExample { public static void main(String[] args) throws IOException { String username = "your_username"; String password = "your_password"; // create URL URL url = new URL("https://example.com/api"); // create connection HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // set request method connection.setRequestMethod("GET"); // set authorization header String auth = username + ":" + password; byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes()); String authHeaderValue = "Basic " + new String(encodedAuth); connection.setRequestProperty("Authorization", authHeaderValue); // send request int responseCode = connection.getResponseCode(); // read response BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // print response System.out.println("Response code: " + responseCode); System.out.println("Response body: " + response.toString()); } } ``` 在上面的代码中,你需要替换掉 `your_username` 和 `your_password` 为你自己的账号和密码。然后你就可以使用 `connection.setRequestProperty` 方法来添加任意的 Header。 请注意,上述示例中的请求方法是 GET,你可以根据你的需要更改为 POST 或其他 HTTP 方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是王小贱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值