A digest authenticator for okhttp. Most of the code is ported from Apache Http Client.
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));
compile 'com.burgstaller:okhttp-digest:0.6'