不支持Android身份验证方案ntlm

时间:2013-12-10 21:06:52

标签: android

我使用asynhttpClient进行基本身份验证

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

这是looj lib ..

下面是我的代码:

usernameRandomPassword = userName +“:”+密码;

            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-Authenticate:NTLM,negotiate = WWW-Authenticate:Negotiate}

我在直接链接上检查了凭据是否正确。

我已经花了一整天的时间,任何人都可以帮助我吗? 如果你分享一些例子,那将非常有帮助。

提前致谢..

4 个答案:

答案 0 :(得分: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);

现在从

下载lib和java文件

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

并将jcifs-1.3.17.jar复制到您的lib文件夹中 JCIFSEngine和NTLMSchemeFactory到你的包。 (如果你愿意,你可以改变包裹。)

这就是你的应用程序已准备就绪。

更有用的链接:

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

答案 1 :(得分:1)

在我看来你可能是代理人的背后? NTLM看起来是一个很大程度上没有文档记录的Microsoft协议:

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

您不能简单地使用Basic Auth,因为这是您正在讲话的服务器所需的一些不同的身份验证方案,或者您和目的地之间的代理。

答案 2 :(得分: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);

你所需要的只是:)

答案 3 :(得分: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。