2013-12-10 37 views
4

我使用asynhttpClient基本認證Android的身份驗證方案NTLM不支持

http://loopj.com/android-async-http/

是裝置100j lib中。

下面

是我的代碼:

usernameRandomPassword =用戶名+「: 「+密碼;下面

  Log.d("username=",usernameRandomPassword); 
      Log.d("url=",url); 
      String authorization = "Basic " + Base64.encodeToString(usernameRandomPassword.getBytes("UTF-8"), Base64.NO_WRAP); 
      httpClient.addHeader("Authorization",authorization); 
      httpClient.addHeader("Content-type", "application/json"); 
      httpClient.setTimeout(20000); 

      httpClient.get(url, new AsyncHttpResponseHandler() { 

        @Override 
        public void onStart() { 
         System.out.println("on satrt"); 
         super.onStart(); 
        } 

        @Override 
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 

         System.out.println("on onSuccess statusCode="+statusCode); 
         toastmessgae("onSuccess status code="+statusCode); 
         super.onSuccess(statusCode, headers, responseBody); 
        } 

        @Override 
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { 

         System.out.println("on onFailure="+statusCode); 
         toastmessgae("onFailure status code="+statusCode); 
         super.onFailure(statusCode, headers, responseBody, error); 

        } 

        @Override 
        public void onFinish() { 
         System.out.println("on onFinish"); 
         super.onFinish(); 
        } 
       }); 



     } catch (UnsupportedEncodingException e) { 

     } 

但我總是在控制檯401接收的日誌不支持

認證方案NTLM。
無法迴應任何這些挑戰:{= NTLM WWW身份驗證:NTLM,洽談= WWW身份驗證:協商}

的憑據是正確的我查了直接的聯繫。

我已經花了這一整天,任何人都可以幫助我嗎? 如果你分享一些例子,它會非常有幫助。

在此先感謝..

回答

10

這裏是通過代碼答案:

下面的代碼添加到您的Android文件

  DefaultHttpClient httpclient = new DefaultHttpClient(); 
      // register ntlm auth scheme 
      httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory()); 
      httpclient.getCredentialsProvider().setCredentials(
        // Limit the credentials only to the specified domain and port 
        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
        // Specify credentials, most of the time only user/pass is needed 
        new NTCredentials(username, password, "", "") 
      ); 

      HttpUriRequest httpget = new HttpGet(your_URL); 
      HttpResponse response = httpclient.execute(httpget); 
      String responseBody = EntityUtils.toString(response.getEntity()); 
      Log.i(tag,"responseBody =>>>>>>>>>>"+responseBody); 

現在從

https://github.com/masconsult/android-ntlm

下載lib和Java文件和複製JCIFS-1.3 .17.jar添加到您的lib文件夾,並將您的軟件包中的JCIFSEngine和NTLMSchemeFactory添加到 。 (如果你願意,你可以改變包裝。)

這就是你的應用程序準備運行。

更多有用的鏈接:

http://www.developergarden.com/en/marketplace/components/details/cmp/android-ntlm-authentication/

+0

鏈接不起作用。另外我得到'getAuthSchemes()'是不確定的HttpClient。 – Darpan

+0

我刪除了httpclient.getAuthSchemes()。register(「ntlm」,new NTLMSchemeFactory());它工作正常。所以我不需要添加庫或NTLMSchemeFactory類 – Hamid

+0

@Hamid它將如何響應NTLM身份驗證挑戰呢?你確定你正在做一個不需要NTLM的web服務嗎? – Sufian

1

在我看來,你可能背後的代理? NTLM看起來是一個主要無證微軟協議:

http://www.innovation.ch/personal/ronald/ntlm.html

你不能簡單地使用基本身份驗證,因爲這是由服務器所需的你是誰講了一些不同的身份驗證方案,或者在你和之間的代理你的目的地。

+0

感謝您的快速回復..你有什麼相同的例子嗎? – morya

0

我有同樣的問題。我想使用android asynk http,但是我找不到ntlm auth 我找到了解決方案: 1)使用上面的答案,下載並導入jcifs-1.3.17.jar 2)然後我下載https://github.com/loopj/android-async-http(不是JAR文件)並導入在我的項目中, 3)然後在文件AsynkHttpClient。java的 後

httpClient = new DefaultHttpClient(cm, httpParams); 

插入

httpClient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory()); 
    httpClient.getCredentialsProvider().setCredentials(
      // Limit the credentials only to the specified domain and port 
      new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
      // Specify credentials, most of the time only user/pass is needed 
      new NTCredentials("username", "pass","", "") 


    ); 

!!!!!!!然後很重要的 必須這樣

httpClient.addRequestInterceptor(new HttpRequestInterceptor() { 
      @Override 
      public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { 
       AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); 
       CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
         ClientContext.CREDS_PROVIDER); 
       HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); 

//    if (authState.getAuthScheme() == null) { 
//     AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); 
//     Credentials creds = credsProvider.getCredentials(authScope); 
//     if (creds != null) { 
//      authState.setAuthScheme(new BasicScheme()); 
//      authState.setCredentials(creds); 
//     } 
//    } 
      } 
     }, 0); 

,所有你需要:)

0

這裏是一個全功能的這種工作發表評論。

try 
    { 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 

    // register ntlm auth scheme 
    httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory()); 

    httpclient.getCredentialsProvider().setCredentials(
      new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
      new NTCredentials("username","password")); 

      //xx = ip address yy = port 
    HttpPost httpPost = new HttpPost("http://xx.xx.xx.xx:yy/"); 

    Log.e(TAG, "executing request" + httpPost.getRequestLine()); 
    HttpResponse response = httpclient.execute(httpPost); 

    HttpEntity entity = response.getEntity(); 

    Log.e(TAG, "" + response.getStatusLine()); 
    if (entity != null) 
    { 
     Log.e(TAG, "Response content length: " + entity.getContentLength()); 
    } 
    if (entity != null) 
    { 
     Log.e(TAG, "Response stream: " + getMessage(entity.getContent())); 
     entity.consumeContent(); 
    } 

    } 
    catch (Exception e) 
    { 
    Log.e(TAG, "" + e.getMessage()); 
    } 

請注意,包括這些進口僅

import java.net.HttpURLConnection; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.auth.AuthScope; 
import org.apache.http.auth.NTCredentials; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.auth.NTLMSchemeFactory; 
import org.apache.http.impl.client.DefaultHttpClient; 

,做只使用HttpClient的,Android的4.3.5.1.jar。