Skip to content Skip to sidebar Skip to footer

Android Httpclient - Getting A File With Preemptive Authentication

I have problems getting the HTML code of a website by using this example code. http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/

Solution 1:

When I tackled this last year, I gave up on HttpClient's native pre-emptive HTTP authentication and just rolled the header myself.

Solution 2:

Alternative 1: Please read Http Basic Authentication with Android that proposes a solution based on the HttpClient 4 official docs. I've not tested it by myself, so I'd be happy to know if it really works.

Edit: I've just tried it and it works like a charm.

Alternative 2: You can also add the "Authorization" HTTP header as proposed by @CommonsWare:

post.addHeader("Authorization", "Basic " + Base64.encode(username+":"+password));

In this case you need a Base64 encoder to encode the string containing the username and the password. You can find a lot of implementations in the Internet.

Solution 3:

For me the example above didn't work on Android. I had to do the following:

post.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.NO_WRAP));

Solution 4:

Thanks janex.

I had to do the same on Android.

post.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(),Base64.NO_WRAP));

cheers

Post a Comment for "Android Httpclient - Getting A File With Preemptive Authentication"