Search in sources :

Example 1 with AuthExchange

use of org.apache.hc.client5.http.auth.AuthExchange in project mercury by yellow013.

the class ClientPreemptiveDigestAuthentication method main.

public static void main(final String[] args) throws Exception {
    try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
        final HttpHost target = new HttpHost("http", "httpbin.org", 80);
        final HttpClientContext localContext = HttpClientContext.create();
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(target), new UsernamePasswordCredentials("user", "passwd".toCharArray()));
        localContext.setCredentialsProvider(credentialsProvider);
        final HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd");
        System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
        for (int i = 0; i < 3; i++) {
            try (final CloseableHttpResponse response = httpclient.execute(target, httpget, localContext)) {
                System.out.println("----------------------------------------");
                System.out.println(response.getCode() + " " + response.getReasonPhrase());
                EntityUtils.consume(response.getEntity());
                final AuthExchange authExchange = localContext.getAuthExchange(target);
                if (authExchange != null) {
                    final AuthScheme authScheme = authExchange.getAuthScheme();
                    if (authScheme instanceof DigestScheme) {
                        final DigestScheme digestScheme = (DigestScheme) authScheme;
                        System.out.println("Nonce: " + digestScheme.getNonce() + "; count: " + digestScheme.getNounceCount());
                    }
                }
            }
        }
    }
}
Also used : DigestScheme(org.apache.hc.client5.http.impl.auth.DigestScheme) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) AuthExchange(org.apache.hc.client5.http.auth.AuthExchange) HttpHost(org.apache.hc.core5.http.HttpHost) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) AuthScope(org.apache.hc.client5.http.auth.AuthScope) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) AuthScheme(org.apache.hc.client5.http.auth.AuthScheme)

Aggregations

AuthExchange (org.apache.hc.client5.http.auth.AuthExchange)1 AuthScheme (org.apache.hc.client5.http.auth.AuthScheme)1 AuthScope (org.apache.hc.client5.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)1 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)1 BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)1 DigestScheme (org.apache.hc.client5.http.impl.auth.DigestScheme)1 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)1 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)1 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)1 HttpHost (org.apache.hc.core5.http.HttpHost)1