正如问题所说,我正在尝试在android中进行摘要式身份验证.
到目前为止,我已经使用了DefaultHttpClient它和它的身份验证方法(使用UsernamePasswordCredentials等等),但是自Android 5以来它已经被弃用,并且将在Android 6中删除.
所以我即将切换DefaultHttpClient到HttpUrlConnection.
我现在想实现摘要式身份验证,这应该工作作为解释非常简单的在这里:
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Run Code Online (Sandbox Code Playgroud)
但是getPasswordAuthentication从来没有因为某些原因而被召唤.
在我搜索这个问题的过程中,我找到了不同的帖子,说HttpUrlConnection在android中不支持摘要认证,但这些帖子是从2010年到2012年,所以我不确定这是否仍然是真的.我们HttpUrlConnection还在桌面java应用程序中使用摘要式身份验证,它可以在其中运行.
我还发现了一些帖子,谈论着OkHttp.OkHttp似乎Android被引擎使用(更具体地说HttpUrlConnectionImpl).但这HttpUrlConnectionImpl有点奇怪,它甚至没有在Eclipse类型层次结构中显示,我无法调试它.它也应该是一个com.squareup.okhttp.internal.huc.HttpUrlConnectionImpl,而在android中它是一个com.android.okhttp.internal.http.HttpUrlConnectionImpl.
所以我只是无法HttpUrlConnection在android中进行摘要认证.
没有外部库,谁能告诉我如何做到这一点?
编辑:
服务器要求摘要验证:
WWW-Authenticate: Digest realm="Realm Name",domain="/domain",nonce="nonce",algorithm=MD5,qop="auth"
Run Code Online (Sandbox Code Playgroud)
所以基本身份验证不应该工作,因为服务器要求摘要.