Android NTLM receiving HTTP / 1.1 401 Unauthorized

I am trying to get data from an exchange point server. Below 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 an HTTP / 1.1 401 response Unauthorized. Even I added all the correct credentials. In the Chrome browser, it works fine with the same credentials. Plz suggest me any changes to the code.

+4
source share
1 answer

Now his job. I changed 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();
+4
source

Source: https://habr.com/ru/post/1661688/


All Articles