I trying to get authenticated from a website. I am using AsyncHttpClient
. Heres the code that I am trying.
Here is my code,
public class LoginActivity extends Activity {
String tag = "LoginActivity";
Button requestBtn;
AsyncHttpClient httpClient = new AsyncHttpClient();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
requestBtn = (Button) findViewById(R.id.upload_file);
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
httpClient.setCookieStore(myCookieStore);
httpClient.setBasicAuth(ApplicationConstants.userName,
ApplicationConstants.password, new AuthScope(
"http://*.*.*.*:8080/someUrl", 8080,
AuthScope.ANY_REALM));
requestBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
httpClient.get("http://*.*.*.*:8080/someurl",new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
Log.d("Sucessful upload","Onsucess" + response);
}
@Override
public void onFailure(Throwable arg0,String arg1) {
Log.d("LoginActivity",arg0.toString());
arg0.printStackTrace();
super.onFailure(arg0, arg1);
}
});
}
}
});
}
}
I get an exception when I tap on the button. It says authentication failed Exception Logcat:
02-27 16:02:42.930: D/LoginActivity(8869): org.apache.http.client.HttpResponseException: Unauthorized
02-27 16:02:42.930: W/System.err(8869): org.apache.http.client.HttpResponseException: Unauthorized
02-27 16:02:42.930: W/System.err(8869): at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:235)
02-27 16:02:42.930: W/System.err(8869): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:79)
02-27 16:02:42.930: W/System.err(8869): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:95)
02-27 16:02:42.930: W/System.err(8869): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:57)
02-27 16:02:42.930: W/System.err(8869): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
02-27 16:02:42.930: W/System.err(8869): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-27 16:02:42.930: W/System.err(8869): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-27 16:02:42.940: W/System.err(8869): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-27 16:02:42.940: W/System.err(8869): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-27 16:02:42.940: W/System.err(8869): at java.lang.Thread.run(Thread.java:856)
What wrong I am doing? The URL I am using is correct.
I came up with another solution, because I was having issues with in-between-proxies that didn't like how the Authorization header was built by using the above proposed solution and also the default (buggy) setBasicAuth(username, password)
implementation of AsyncHttpClient.
So I added the header myself, doing the BASE64 username/password encoding myself, but using the addHeader()
method inherited from the parent client implementation.
It works now.
asyncHttpClient.addHeader(
"Authorization",
"Basic " + Base64.encodeToString(
(username+":"+password).getBytes(),Base64.NO_WRAP)
);
I hope this helps somebody.