Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
4.2.1
-
None
Description
the following code (taken from http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html section 4.8 and modified to use a URI) will not add authentication headers to the outgoing http request because the URI string does not explicitely specify the port:
URI uri = new URI("http://somedomain.com/stuff");
HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("username", "password"));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
HttpGet httpget = new HttpGet(uri);
for (int i = 0; i < 3; i++)
the root cause for this is in RequestAuthCache.java line 90:
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (target.getPort() < 0)
AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
if (target != null && targetState != null && targetState.getState() == AuthProtocolState.UNCHALLENGED) {
AuthScheme authScheme = authCache.get(target);
if (authScheme != null)
}
the target has no port (meaning <0 ), so its recreated with the default http scheme port of 80.
meanwhile authCache uses the original target host as key, and so authScheme will be null.
explicitely declaring port 80 in the URI string works around this, but i think this should work by default.
Attachments
Issue Links
- is duplicated by
-
HTTPCLIENT-1242 Preemptive authentication using AuthCache stopped working
- Closed