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: [email protected] For additional commands, e-mail: [email protected]
