Hi,

I use the HttpCacheContext but I think with the httpclient5 the preemtive 
authentication is implemented the same way.


        public HttpCacheContext getHttpCacheContext(final URL aURL) throws 
Exception {
                final HttpHost targetHost = new HttpHost(aURL.getProtocol(), 
aURL.getHost(), aURL.getPort()==-1?aURL.getDefaultPort():aURL.getPort());
                final HttpCacheContext context = HttpCacheContext.create();
                final AuthCache authCache = new BasicAuthCache();
                if (this.getPreAuth() && this.getUser()!= null && 
this.getPassword()!=null ) {
                        if (this.getAuthScheme() == AuthScheme.BASIC) {
                                final BasicScheme basicAuth = new BasicScheme();
                                basicAuth.initPreemptive(
                                                new 
UsernamePasswordCredentials(this.getUser(), this.getPassword().toCharArray()));
                                authCache.put(targetHost, basicAuth);
                        } else if (this.getAuthScheme() == AuthScheme.DIGEST) {
                                final DigestScheme digestScheme = new 
DigestScheme();
                                final UsernamePasswordCredentials creds = new 
UsernamePasswordCredentials(this.replace(this.getUser()),
                                                
this.replace(this.getPassword()).toCharArray());
                                digestScheme.initPreemptive(creds, 
UUID.randomUUID().toString().replaceAll("-", ""), "vectorius");
                                authCache.put(targetHost, digestScheme);
                        }
                        context.setAuthCache(authCache);
                }
                
context.setCredentialsProvider(this.getCredentialsProvider(aURL));
                return context;
        }

regards,

Peter

-----Ursprüngliche Nachricht-----
Von: Christophe Darville <c...@internetvista.com> 
Gesendet: Mittwoch, 9. Februar 2022 17:00
An: HttpClient User Discussion <httpclient-users@hc.apache.org>
Betreff: Preemptive authentication in Http Client 5 Async

Hi,

I am trying to make preemptive authentication in HttpClient 5 Async mode using 
the following code, but I does not work, first request is still without 
authentication and the authentication is done on the second request : 

        HttpClientContext httpContext = HttpClientContext.create();
        CredentialsProvider credentialsProvider = getCredentialsProvider();
        if (preemptive) {
                URI uri = URI.create(url);
                HttpHost targetHost = new HttpHost(uri.getScheme(), 
uri.getHost(), uri.getPort());
                AuthCache authCache = new BasicAuthCache();
                BasicScheme basicAuth = new BasicScheme();
                authCache.put(targetHost, basicAuth);
                httpContext.setAuthCache(authCache);
        }
        httpContext.setCredentialsProvider(credentialsProvider);

This code was working in httpClient 4. Any suggestion on how to make preemptive 
authentication work in HttpClient5 Async ?

Thank you,
Christophe




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

Reply via email to