【发布时间】:2018-10-09 05:15:47
【问题描述】:
我正在尝试使用 HttpURLConnection 在我的请求中发送多个标头(接受和身份验证密钥),但只发送第一个。
URL url = new URL(fileOrUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
for (String h : getHeaders()) {
String[] keyval = h.split(":");
if (keyval.length != 2) {
throw new IllegalArgumentException();
}
System.out.println("Setting header " + keyval[0] + ": " + keyval[1]);
conn.addRequestProperty(keyval[0], keyval[1]);
}
System.out.println(conn.getRequestProperties());
return stream = conn.getInputStream();
这会导致:
Setting header Accept: application/x-google-protobuf
Setting header Authorization: apikey
{Accept=[ application/x-google-protobuf]}
为什么只发送一个标头?
【问题讨论】:
-
我认为这不是重复的——有人可能不会密切关注 RTFM 并将授权标头从 getRequestProperties 中过滤掉的事实联系起来。这是相同的答案,但不一定是相同的问题。
标签: java