gpt4 book ai didi

java - 附加的 http header 不起作用

转载 作者:行者123 更新时间:2023-12-01 14:21:32 24 4
gpt4 key购买 nike

我正在尝试将 header 添加到 http 中并在我的代码中添加 header -

URL url = new URL("http://localhost:8080/share/page"); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("SsoUserHeader", "admin");
conn.addRequestProperty("mysso", "hi");
conn.setRequestProperty("Authorization",basicAuth );
conn.connect();

Map<String, List<String>> hdrs = conn.getHeaderFields();
Set<String> hdrKeys = hdrs.keySet();
for (String k : hdrKeys)
System.out.println("Key: " + k + " Value: " + hdrs.get(k));

System.out.println("Header1: "+conn.getHeaderField("SsoUserHeader"));
System.out.println("Header2: "+conn.getHeaderField("mysso"));
System.out.println("Header3: "+conn.getHeaderField("Authorization"));

但是当我尝试打印这些 header 的值时,它会显示为空。请让我知道如何获取 header 值。我得到的输出

Key: null  Value: [HTTP/1.1 200 OK]
Key: Content-Language Value: [en-US]
Key: Transfer-Encoding Value: [chunked]
Key: Date Value: [Sun, 07 Jul 2013 05:30:45 GMT]
Key: Set-Cookie Value: [JSESSIONID=E6BCB7632619ABF41D1A7C6D7CDC53E4;
Path=/share/; HttpOnly]

Key: Content-Type Value: [text/html;charset=utf-8]
Key: Server Value: [Apache-Coyote/1.1]
Key: Cache-Control Value: [no-cache]

Header1: null
Header2: null
Header3: null

最佳答案

getHeaderFields() 返回在 HTTP 响应中发送的 header 字段,而不是在 HTTP 请求中发送的 header 字段。您从未发送过这些 header :Content-Language、Transfer-Encoding 等,但您在 getHeaderFields() 中获取了它们。这是因为服务器发送了这些 header 。请求中设置的自定义 header 字段将可用于 http://localhost:8080/share/page

/page 可以使用以下方式访问这些 header 字段:

request.getHeader("SsoUserHeader"); //admin
request.getHeader("mysso"); //hi
request.getHeader("Authorization");

关于java - 附加的 http header 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17509632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com