如何做HTTP认证的机器人? [英] How to do HTTP authentication in android?
问题描述
我检查出类org.apache.http.auth。 任何更多的参考,或例如,如果任何人有?
I am checking out the class org.apache.http.auth. Any more reference or example if anyone has?
推荐答案
我还没有遇到那个特定的软件包之前,但它说,这是对客户端的HTTP验证,我已经能够用做在Android java.net
的API,像这样:
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:
Authenticator.setDefault(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myuser","mypass".toCharArray());
}});
HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
c.setUseCaches(false);
c.connect();
显然,你的getPasswordAuthentication()或许应该做一些比返回一个常数更智能。
Obviously your getPasswordAuthentication() should probably do something more intelligent than returning a constant.
如果你想使一个机构(例如 POST
)身份验证的要求,提防的 Android的问题,4326 。我挂一个修复建议,以有平台,但有一个简单的解决方法,如果你只是想基本身份验证:不验证程序打扰,而是做到这一点:
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:
c.setRequestProperty("Authorization", "basic " +
Base64.encode("myuser:mypass".getBytes()));
这篇关于如何做HTTP认证的机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!