我正在尝试连接到受摘要身份验证保护的网站。如果我尝试通过 Insomnia 或 Firefox 登录,但我无法让它在 Java 17 中工作(Insomnia 自动生成的代码也不起作用),我的凭据工作正常。
我尝试遵循并理解以下教程/文档:
https://www.baeldung.com/java-9-http-client
https://docs.oracle.com/javase/7/docs/technotes/guides/net/http-auth.html
据我了解,两者都提到支持摘要。
我得到的结果始终是状态代码 401 和摘要身份验证失败时的预期标头:
www-authenticate=[摘要领域=“api-realm”,qop=“auth”,nonce=“NONCE==”
这是当前的代码。getPasswordAuthentication方法不会被执行:
public void checkIsAPIRunning() {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://the-site-I-try-to-connect-with:443/api/function"))
.method("GET", HttpRequest.BodyPublishers.noBody()).build();
HttpResponse<String> response = null;
try {
response = HttpClient.newBuilder().authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "pass".toCharArray());
}
}).build().send(request, BodyHandlers.ofString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我是否误解了文档?我将不胜感激任何帮助或指示:)