如何在android中进行HTTP身份验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1968416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do HTTP authentication in android?
提问by Bohemian
I am checking out the class org.apache.http.auth. Any more reference or example if anyone has?
我正在检查类 org.apache.http.auth。如果有人有更多参考或示例吗?
回答by cesards
For me, it worked,
对我来说,它奏效了,
final String basicAuth = "Basic " + Base64.encodeToString("user:password".getBytes(), Base64.NO_WRAP);
Apache HttpCLient:
Apache Http客户端:
request.setHeader("Authorization", basicAuth);
HttpUrlConnection:
HttpUrl 连接:
connection.setRequestProperty ("Authorization", basicAuth);
回答by Chris Boyle
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:
我以前没有遇到过那个特定的包,但它说它用于客户端 HTTP 身份验证,我已经能够使用java.net
API在 Android 上执行此操作,如下所示:
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();
Obviously your getPasswordAuthentication() should probably do something more intelligent than returning a constant.
显然,您的 getPasswordAuthentication() 可能应该做一些比返回常量更智能的事情。
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:
如果您尝试使用POST
带有身份验证的正文(例如)发出请求,请注意Android 问题 4326。我已将建议的修复程序链接到那里的平台,但如果您只需要基本身份验证,则有一个简单的解决方法:不要打扰 Authenticator,而是执行以下操作:
c.setRequestProperty("Authorization", "basic " +
Base64.encode("myuser:mypass".getBytes(), Base64.NO_WRAP));
回答by Dmitry Sitnikov
You can manually insert http header to request:
您可以手动插入 http 标头来请求:
HttpGet request = new HttpGet(...);
request.setHeader("Authorization", "Basic "+Base64.encodeBytes("login:password".getBytes()));
回答by GWu
Manual method works well with import android.util.Base64, but be sure to set Base64.NO_WRAP on calling encode:
手动方法适用于 import android.util.Base64,但一定要在调用 encode 时设置 Base64.NO_WRAP:
String basicAuth = "Basic " + new String(Base64.encode("user:pass".getBytes(),Base64.NO_WRAP ));
connection.setRequestProperty ("Authorization", basicAuth);
回答by Oke Uwechue
For my Android projects I've used the Base64 library from here:
对于我的 Android 项目,我从这里使用了 Base64 库:
It's a very extensive library and so far I've had no problems with it.
这是一个非常广泛的库,到目前为止我没有遇到任何问题。
回答by Cristiana Chavez
This works for me
这对我有用
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setRequestProperty("Authorization", "basic " +
Base64.encode("username:password".getBytes()));
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();