I am checking out the class org.apache.http.auth. Any more reference or example if anyone has?
-
2Is this a question about Android applications authentication or just about authentication for a general web app, which just might run on Android?jottos– jottos2009-12-28 08:02:08 +00:00Commented Dec 28, 2009 at 8:02
-
For web authentication(http authentication) for user credentials(username,password)Bohemian– Bohemian2009-12-28 08:18:34 +00:00Commented Dec 28, 2009 at 8:18
6 Answers
For me, it worked,
final String basicAuth = "Basic " + Base64.encodeToString("user:password".getBytes(), Base64.NO_WRAP);
Apache HttpCLient:
request.setHeader("Authorization", basicAuth);
HttpUrlConnection:
connection.setRequestProperty ("Authorization", basicAuth);
8 Comments
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();
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:
c.setRequestProperty("Authorization", "basic " +
Base64.encode("myuser:mypass".getBytes(), Base64.NO_WRAP));
9 Comments
android.util. I was using ksoap2-android when I found this, and they have an implementation that depends only on java.io, so you could just grab that class (subject to its license of course) from: kobjects.cvs.sourceforge.net/kobjects/kobjects/src/org/kobjects/…For my Android projects I've used the Base64 library from here:
It's a very extensive library and so far I've had no problems with it.
Comments
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();