android - 将 OkHTTP 3 与摘要一起使用

标签 android okhttp digest

因此,为了使用 OkHTTP 的摘要式身份验证,我需要使用 digest plugin或者自己实现,所以我自然而然地使用了插件。然而,存储库自述文件中的示例用法是针对以前版本的 OkHTTP 的,我不能使用相同的代码在版本 3 中实现它。我试图查看一些 their tests。 (使用 OkHTTP 3)查看代码如何工作,但我不确定。

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

最佳答案

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();
}

关于android - 将 OkHTTP 3 与摘要一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42036236/

相关文章:

java - 当我们有 OkHttp 时为什么要使用 Retrofit

java - 未知主机异常 : name or service not known

python - Python 中的摘要式身份验证?

pdf - PKCS#7 中的消息消化过程(PDF 签名)

android - Google Play 服务库未安装

java - 执行onTaskRemoved后应用崩溃

java - OkHttp 2.0 手动验证正在使用的密码套件

r - 为什么NaN和Inf-Inf的哈希值不同?

android - 立即拍摄2张照片-使应用程序崩溃

java - 当其他按钮未按下时,保持按钮按下状态