gpt4 book ai didi

java - HttpsURLConnection 验证两次?

转载 作者:可可西里 更新时间:2023-11-01 17:06:11 24 4
gpt4 key购买 nike

我建立了一个安全的 http 连接,然后尝试从中获取 InputStream。发生连接,我能够获取数据,但我实际上是在向服务器发送两个授权请求??这是我获取连接并建立输入流的代码:

someConnection = (HttpsURLConnection) url.openConnection();
String userPass = username + ":" + password;
String basicAuth = "Basic" + new String(new Base64().encode(userPass.getBytes()));
someConnection.setRequestProperty("Authorization", basicAuth);
if (header != null) someConnection.setRequestProperty(header, headerValue);
InputStream is = someConnection.getInputStream();

在调用 .getInputStream() 方法之前没有流量。然后我看到两个授权请求:

Wireshark output

知道它为什么这样做吗?第一个请求似乎由于某种原因失败了。

最佳答案

您的 header Authorization 的值与预期格式不匹配,它应该是 "Basic " 后跟 ${username}:${ password} 使用 Base 64 的 RFC2045-MIME 变体编码(有关 Basic access authentication 的更多详细信息)。

在这里您忘记在 Basic 之后添加尾随空格,这样身份验证就永远不会正确完成,从而导致这种意外行为。

关于java - HttpsURLConnection 验证两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41703157/

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