我正在使用apache HttpClient5实现一个简单的(不是抢占式,而不是通过代理)摘要式身份验证。文档很少,但应该相当简单。
但是,我一直得到401作为回应,而且我不清楚我是如何确定的,如果这是因为我的实施错误,或者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.