如何在android中进行HTTP认证?
我正在查看 org.apache.http.auth 类。 如果有人有更多参考或例子吗?
I am checking out the class org.apache.http.auth.
Any more reference or example if anyone has?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对我来说,它有效,
Apache HttpCLient:
HttpUrlConnection:
For me, it worked,
Apache HttpCLient:
HttpUrlConnection:
我以前没有遇到过那个特定的包,但它说它用于客户端 HTTP 身份验证,我已经能够使用
java.net
API 在 Android 上完成此操作,如下所示: getPasswordAuthentication() 可能应该做一些比返回常量更智能的事情。
如果您尝试通过身份验证发出包含正文(例如
POST
)的请求,请注意 Android 问题 4326。我已将建议的修复链接到该平台,但如果您只需要基本身份验证,有一个简单的解决方法:不要使用身份验证器,而是执行以下操作:I've not met that particular package before, but it says it's for client-side HTTP authentication, which I've been able to do on Android using the
java.net
APIs, like so:Obviously your getPasswordAuthentication() should probably do something more intelligent than returning a constant.
If you're trying to make a request with a body (e.g.
POST
) with authentication, beware of Android issue 4326. I've linked a suggested fix to the platform there, but there's a simple workaround if you only want Basic auth: don't bother with Authenticator, and instead do this:您可以手动插入http header来请求:
You can manually insert http header to request:
手动方法与 import android.util.Base64 配合使用效果很好,但请务必在调用编码时设置 Base64.NO_WRAP:
Manual method works well with import android.util.Base64, but be sure to set Base64.NO_WRAP on calling encode:
我使用了这里的 Base64 库:
http://iharder.net/base64
对于我的 Android 项目, 一个非常广泛的图书馆,到目前为止我还没有遇到任何问题。
For my Android projects I've used the Base64 library from here:
http://iharder.net/base64
It's a very extensive library and so far I've had no problems with it.
这对我有用
This works for me