views:

1964

answers:

2

I am checking out the class org.apache.http.auth. Any more reference or example if anyone has?

+4  A: 

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()));
Chris Boyle
Do u know any base64 encoding class present in android 2.0??
Bohemian
The platform has it in a few places, but oddly enough they don't expose it. They even left references to it in the docs of e.g. `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: http://kobjects.cvs.sourceforge.net/kobjects/kobjects/src/org/kobjects/base64/Base64.java?view=markup
Chris Boyle
A: 

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.

Oke Uwechue