使用OkHTTP 3与摘要认证

3
因此,为了在OkHTTP中使用摘要身份验证,我需要使用摘要插件或自行实现,因此我自然而然地使用了插件。但是,存储库自述文件中的示例用法适用于先前版本的OkHTTP,我无法使用相同的代码在版本3中实现它。我尝试查看一些测试(使用OkHTTP 3)以查看代码如何工作,但我不确定。

那么,我该如何在最新版本的OkHttp中使用这个http摘要插件呢?


1
你为什么认为这是针对旧版OkHttp的?代码都从“okhttp3”包中导入。 - Jesse Wilson
@JesseWilson,你说得对,测试确实使用了okhttp3,但是自述文件中的示例仍然使用了旧版本的方法,例如client.setAuthentication。从测试中我不确定最新版本中哪些代码替换了已经不存在的方法。 - user2844358
1个回答

2
import com.burgstaller.okhttp.AuthenticationCacheInterceptor;
import com.burgstaller.okhttp.CachingAuthenticatorDecorator;
import com.burgstaller.okhttp.digest.CachingAuthenticator;
import com.burgstaller.okhttp.digest.Credentials;
import com.burgstaller.okhttp.digest.DigestAuthenticator;
import okhttp3.OkHttpClient;

private OkHttpClient buildHttpClient(String username, String password) {
    // Library used for digest authenticaton: https://github.com/rburgst/okhttp-digest
    var authenticator = new DigestAuthenticator(new Credentials(username, password));
    var authCache = new ConcurrentHashMap<String, CachingAuthenticator>();

    var decorator = new CachingAuthenticatorDecorator(authenticator, authCache);
    var interceptor = new AuthenticationCacheInterceptor(authCache);

    return httpClient.newBuilder()
        .authenticator(decorator)
        .addInterceptor(interceptor)
        .build();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接