2016-11-23 107 views
4

我嘗試從共享點服務器獲取數據。 以下是我的代碼。Android NTLM獲取HTTP/1.1 401未授權

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建議我在代碼中進行任何更改。

+0

您可能不需要對代碼進行任何更改。在IE中,在工具>高級下,確保您已啓用「集成Windows身份驗證」,在工具>安全>本地Intranet區域>站點>高級下,確保列出目標網站。清除Cookie並重新啓動IE。 –

回答

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