Java 使用ApacheHttpClient进行摘要身份验证
我正在使用ApacheHttpClient5实现一个简单的(不是先发制人的,不是通过代理的)摘要身份验证。文档非常稀少,但应该相当简单 然而,我一直得到401作为回应,我不清楚如何确定这是因为我的实施错误,还是401是真的。我检查过,使用的凭证绝对正确 因此,我的问题是:Java 使用ApacheHttpClient进行摘要身份验证,java,apache,authentication,Java,Apache,Authentication,我正在使用ApacheHttpClient5实现一个简单的(不是先发制人的,不是通过代理的)摘要身份验证。文档非常稀少,但应该相当简单 然而,我一直得到401作为回应,我不清楚如何确定这是因为我的实施错误,还是401是真的。我检查过,使用的凭证绝对正确 因此,我的问题是: 这段代码基本正确还是我遗漏了什么(例如,我是否必须自己处理挑战。我假设httpClient为我做这件事) 如果质询处理失败是因为处理不正确,那么除了401之外,我不应该在日志中获取更多信息 摘要{}身份验证错误:缺少领域:这在
HttpRequestBase request = request = new HttpGet(params.getRequestUrl());
// .....
// then setting requestconfig, headers, body, contenttype etc.
// .....
HttpClientContext localContext = HttpClientContext.create();
// set authentication (just a POJO with a username and a password)
final HttpBasicAuthentication authentication = params.getAuthentication();
URI uri = request.getURI();
String host = uri.getHost();
int port = uri.getPort();
String scheme = uri.getScheme();
HttpHost target = new HttpHost(host,port,scheme);
char[] password = authentication.getPassword().toCharArray();
final org.apache.hc.client5.http.auth.Credentials creds = new org.apache.hc.client5.http.auth.UsernamePasswordCredentials(authentication.getUsername(), password);
BasicCredentialsProvider cP = new BasicCredentialsProvider();
cP.setCredentials(org.apache.hc.client5.http.auth.AuthScope.ANY, creds);
localContext.setCredentialsProvider(cP);
AuthCache authCache = new BasicAuthCache();
DigestScheme digestScheme = new DigestScheme();
authCache.put(target, digestScheme);
localContext.setAuthCache(authCache);
// Tell the HTTP server that we will close the connection after the response
request.addHeader("Connection", "close");
// http response
final HttpServiceResponse response = new HttpServiceResponse();
// Execute the method.
CloseableHttpResponse closeableResponse = httpClient5.execute(request, localContext);
日志记录
... sorry... I had to remove the logging. because of security conserns.
不关闭连接没有多大区别。可能重复的不关闭连接没有多大区别。可能重复的