5

Currently I am using a DefaultHttpClient with ThreadSafeClientConnManager. This works fine, but I would like to replace this by using AndroidHttpClient. Unfortunately I am not able to add UsernamePasswordCredentials which is currently important for me. Can anyone provide a tip or solution?

user229044
  • 232,980
  • 40
  • 330
  • 338

3 Answers3

0

I know the question is old but for the benefit of anyone stumbling on this (like I did), you can roll the header yourself with HttpGet object. Like so :

httpGet.addHeader("Authorization", "Basic " + Base64.encode(username+":"+password));
Saad Farooq
  • 13,172
  • 10
  • 68
  • 94
0

Some enhancement to Saad Farooq's answer, the following code works for me.

final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");

getRequest = new HttpGet(url);

getRequest.addHeader("Authorization", "Basic " + Base64.encodeToString(new
                String(username + ":" + password).getBytes(), Base64.NO_WRAP));
chunyap
  • 101
  • 1
  • 4
0

You need to use HttpRequestInterceptor class for authentication.

Here is an example

HttpRequestInterceptor httpRequestInterceptor = new HttpRequestInterceptor() {
    public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
        AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
                ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

        if (authState.getAuthScheme() == null) {
            AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
            Credentials creds = credsProvider.getCredentials(authScope);
            if (creds != null) {
                authState.setAuthScheme(new BasicScheme());
                authState.setCredentials(creds);
            }
        }
    }    
};
Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
  • This opens a new question: How to add the interceptor to the AndroidHttpClient? – lichtzeichenanlage Jan 29 '11 at 12:08
  • Can you please let me know why are you trying to use `AndroidHttpClient`? – Tanmay Mandal Jan 29 '11 at 21:13
  • Hey Tammay. On the one hand the code looks cleaner to me, on the other hand I try to use standard classes instead of writing and maintaining my one once. Are those reasons valid? – lichtzeichenanlage Jan 30 '11 at 00:52
  • If you want to use standard classes use `HttpClient` – Tanmay Mandal Feb 01 '11 at 16:38
  • You didn't got me. I am not talking about standard classes, I am talking about selfwriten code. Unless AndroidHttpClient does provide threadsafe connection manger, connection pools and is watching memory leaks,it eliminates code I have otherwise to maintain on my own. – lichtzeichenanlage Feb 01 '11 at 16:51
  • Plus `HttpClient` is known to cause hanging problem. See http://stackoverflow.com/questions/9505358/android-httpclient-hangs-on-second-request-to-the-server-connection-timed-out – Saad Farooq Apr 04 '12 at 20:37