Android NTLM获取HTTP / 1.1 401未经授权

时间:2016-11-23 09:18:02

标签: java android authentication sharepoint ntlm

我尝试从共享点服务器获取数据。 以下是我的代码。

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

我有HTTP / 1.1 401未经授权的回应。即使我已经添加了所有权利凭证。 在Chrome浏览器中它使用相同的凭据工作正常。 plz建议我改变代码。

1 个答案:

答案 0 :(得分:4)

现在它的工作。 我用标题修改了我的代码。

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