Rahul Giradkar Rahul Giradkar - 8 days ago 5
Java Question

Android NTLM getting HTTP/1.1 401 Unauthorized

I am try to get data from share point server.
Following is my code.

DefaultHttpClient httpclient = new DefaultHttpClient();

httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
httpclient.getCredentialsProvider().setCredentials(new AuthScope("masconsult.eu", -1),
new NTCredentials(username, password, "", ""));
HttpGet httpGet = new HttpGet(webserviceUrl);
httpGet.addHeader("Content-type", "application/json");
httpGet.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
// HttpResponse response="";
String responseXML = "";
HttpResponse response = httpclient.execute(httpGet);
response.getStatusLine().getReasonPhrase();
responseXML = EntityUtils.toString(response.getEntity());
Toast.makeText(this, responseXML, Toast.LENGTH_LONG).show();


I have got response HTTP/1.1 401 Unauthorized. Even I have added all right credential.
In Chrome browser It working fine with same credential.
plz suggest me any change in code.

Answer

Now its working. I modified my code with headers.

 DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
    httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new NTCredentials(username, password, "", ""));
    HttpGet httpGet = new HttpGet(webserviceUrl);
    httpGet.addHeader("accept", "application/json;odata=verbose");
    httpGet.addHeader("content-Type", "application/json;odata=verbose");
    httpGet.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
    HttpResponse response = httpclient.execute(httpGet);
    System.out.println("Responseeee" + response.getStatusLine());
    responseXML = EntityUtils.toString(response.getEntity());
    new JSONObject(responseXML).toString();