Tuesday, 3 September 2013

Authentication error: Unable to respond to any of these challenges: {} on POST - Android

Authentication error: Unable to respond to any of these challenges: {} on
POST - Android

I can't seem to authenticate this POST. My GET is the same authentication
method and it works. I've tried various methods to authenticate but I get
the same error. If anyone sees anything I've overlooked, let me know.
URL url = null;
try {
url = new URL("someurl");
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
HttpURLConnection connection;
String ret = null;
try {
connection = (HttpURLConnection)url.openConnection();
byte[] auth = ("username" + ":" + "password").getBytes();
ret="Basic " + Base64.encodeToString(auth, Base64.NO_WRAP);
connection.setRequestProperty("Authorization", ret);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpConnectionParams.setConnectionTimeout(client.getParams(),
10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost(ENDPOINT);
json.put("FirstName", data[0]);
json.put("LastName", data[1]);
json.put("Phone", data[2]);
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post.setEntity(se);
response = client.execute(post);
response.setHeader(new BasicHeader("Authorization", ret));
// Checking response
if(response!=null){
InputStream in = response.getEntity().getContent();
//Get the data in the entity
in.close();
}
} catch(Exception e) {
e.printStackTrace();
}

No comments:

Post a Comment