我写了下面的HttpClient代码,但是它没有发送Authorization
头到服务器:
public static void main(String[] args) {
var client = HttpClient.newBuilder()
.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password".toCharArray());
}
})
.version(HttpClient.Version.HTTP_1_1)
.build();
var request = HttpRequest.newBuilder()
.uri("https://service-that-needs-auth.example/")
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
我从调用的服务中得到了一个HTTP 401错误,在我的情况下,这是Atlassian Jira Cloud API。
我确认HttpClient没有调用我的getPasswordAuthentication()
方法。
为什么它不起作用,我应该做些什么?
curl -v https://example.atlassian.net/rest/agile/1.0/board/44/issue
以查看响应头,在这种情况下HttpClient将处理这些响应头。 - Jonathan Fuerth13
:| 今天我们遇到了这个问题,我一直对开发人员说显然他做错了什么,结果我错得离谱... - Eugene