Skip to content

bird-frank/okhttp-digest

 
 

Repository files navigation

okhttp-digest

A digest authenticator for okhttp. Most of the code is ported from Apache Http Client.

Usage

        client = new OkHttpClient();
        final DigestAuthenticator authenticator = new DigestAuthenticator();
        authenticator.setCredentials(new Credentials("username", "pass"));

        final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
        client.interceptors().add(new AuthenticationCacheInterceptor(authCache));
        client.setAuthenticator(new CachingAuthenticatorDecorator(authenticator, authCache));

        Request request = new Request.Builder()
          .url(url);
          .get()
          .build();
        Response response = client.newCall(request).execute();

If you want to support multiple authentication schemes (including auth caching) then this should work:

        client = new OkHttpClient();
        final Map<String, String> authCache = new ConcurrentHashMap<>();

        Credentials credentials = new Credentials("username", "pass");

        final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
        final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);

        DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder()
                .with("Digest", digestAuthenticator)
                .with("Basic", basicAuthenticator)
                .build();

        client.interceptors().add(new AuthenticationCacheInterceptor(authCache));
        client.setAuthenticator(new CachingAuthenticatorDecorator(authenticator, authCache));

Download Build Status

Use via gradle

compile 'com.burgstaller:okhttp-digest:0.6'

About

a digest authenticator for okhttp

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 97.4%
  • Shell 2.6%