Misha Bhardwaj
Misha Bhardwaj

Reputation: 1387

HttpClient Authentication in Android

I am trying to access a webservice from android which requires Full authentication. I am using Basic http authentication for this purpose.

I have tried setting UserNamePasswordCredentials as well as setting headers in HttpGet request but all in vein. Please tell me what I am doing wrong.

The code I am using for setting headers in HttpGet request:

HttpGet requestLogin = new HttpGet(url);               
requestLogin.addHeader("Authorization", "Basic " + Base64.encodeToString((username+":"+password).getBytes(), Base64.NO_WRAP));

The code I am using to set UserNamePassword credentials:

DefaultHttpClient httpClient = new DefaultHttpClient(mgr, tempClient.getParams());
httpClient.getCredentialsProvider().setCredentials(
                   AuthScope.ANY,
                    new UsernamePasswordCredentials(username,password));

This is exception I am getting while using this code:

05-26 17:21:22.234: E/res(1651): <html><head><title>Apache Tomcat/6.0.35 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 401 - Bad credentials</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Bad credentials</u></p><p><b>description</b> <u>This request requires HTTP authentication (Bad credentials).</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.35</h3></body></html>

When I use these same credentials on browser, it works. So the credentials are not incorrect. Please let me know what I am doing wrong. I am totally stuck in it.

Thanks in advance.

Upvotes: 3

Views: 5332

Answers (1)

Torben Kohlmeier
Torben Kohlmeier

Reputation: 6783

I just tried the manual http header variant, it should work with Base64.Default flag:

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

Upvotes: 3

Related Questions