hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Preemptive authentication examples wrong
Date Mon, 29 Aug 2011 18:21:10 GMT
On Sun, 2011-08-28 at 11:51 -0700, Ken Krugler wrote:
> Hi Oleg,
> 
> We had a situation where we needed to hit a client's test server that would only work
with preemptive authentication.
> 
> Below is the code that we wound up using. Given all of the back-and-forth on the list
about this functionality, I'm hoping you can let me know if this is anywhere close...
> 
> Thanks!
> 
> -- Ken
> 

Ken

This code should work with all version of HttpClient 4.x. However, as of
version 4.1 the same can be done in a much simpler way by pre-populating
the auth cache: 

http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java


Hope this helps

Oleg

> 
>         HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
>             public void process(HttpRequest request, HttpContext context) throws HttpException,
IOException {
>                 AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
>                 CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
>                         ClientContext.CREDS_PROVIDER);
>                 HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
>                 
>                 if (authState.getAuthScheme() == null) {
>                     AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
>                     Credentials creds = credsProvider.getCredentials(authScope);
>                     if (creds != null) {
>                         authState.setAuthScheme(new BasicScheme());
>                         authState.setCredentials(creds);
>                     }
>                 }
>             }
>         };
> 
> --------------------------
> Ken Krugler
> +1 530-210-6378
> http://bixolabs.com
> custom data mining solutions
> 
> 
> 
> 
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message