Android NTLM getting HTTP/1.1 401 Unauthorized

Rahul Giradkar

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.

Rahul Giradkar

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();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

HTTP response '401: Unauthorized' using NTLM with Wildfly

getting TokenResponseException: 401 Unauthorized

getting HTTP Status 401 - unauthorized user error for ebayclassifeids Api

Getting 401 unauthorized error in retrofit?

Getting 401 unauthorized status in an API

OAuth1 reject_token 401 unauthorized

Axios with OMDB API, getting 401 Unauthorized

Getting error 401 - unauthorized with google API

Solr HTTP error: Unauthorized (401)

Unauthorized (HTTP 401) R omdbapi

Webservice not working in Android Retrofit , but works in Postman and Swift / iOS, Getting 401 Unauthorized

getting status -1 instead 401 Angularjs

Android Push Notification unauthorized error 401

Http failure response for http url: 401 Unauthorized

python urllib2 and ntlm - getting '<h1>Object Moved</h1>' in response html

403 Forbidden vs 401 Unauthorized HTTP responses

Django Rest Framework: HTTP 401 Unauthorized error

Getting Unauthorized Error 401 in Firebase Cloud Messaging in cURL command

Why am I getting a "401 Unauthorized" error in Maven?

Spring boot getting 401 unauthorized status code for simple get request

Getting ErrImagePull : 401 Unauthorized when creating a Kubernetes Pod with a declarative command

Getting the 401 Unauthorized while setting User permissions on fastAPI delete method

Getting 401 Unauthorized error while requasting GitHub API

HTTP Error 401: Unauthorized using urllib.request.urlopen

Azure AD - HTTP/1.1 401 Unauthorized - Windows Service

catch a 401 Unauthorized http response using jquery ajax

HTTP 401 Unauthorized error occurs in Spring Boot test

GitHub Actions with hub results in Unauthorized (HTTP 401) Bad credentials

HTTPError: HTTP Error 401: Unauthorized for sendgrid integration with python