人們可以使用RequestConfig
RequestConfig requestConfig = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
.build();
本地執行上下文包含關於請求執行包括對目標和代理主機
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpClientContext localContext = HttpClientContext.create();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget, localContext);
try {
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
AuthState targetAuthState = localContext.getTargetAuthState();
if (targetAuthState.getAuthScheme() != null) {
System.out.println("Target auth scheme: " +
targetAuthState.getAuthScheme().getSchemeName());
}
AuthState proxyAuthState = localContext.getProxyAuthState();
if (proxyAuthState.getAuthScheme() != null) {
System.out.println("Proxy auth scheme: " +
proxyAuthState.getAuthScheme().getSchemeName());
}
} finally {
response.close();
}
} finally {
httpclient.close();
}
確定AUTH狀態的所有細節配置基於每個請求基礎優選AUTH方案,我如何才能獲得最後一個成功的計劃名稱? –
一個通常應該沒有必要這樣做。 HttpClient自動重新使用最後一次已知的成功認證方案_provided_請求共享相同的執行上下文 – oleg