AsyncHttpClient验证失败 [英] AsyncHttpClient Authentication failed

查看:695
本文介绍了AsyncHttpClient验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个网站进行身份验证。我使用 AsyncHttpClient 。继承人的code,我想。

下面是我的code,

 公共类LoginActivity延伸活动{

    字符串变量=LoginActivity;
    按钮requestBtn;
    AsyncHttpClient的HttpClient =新AsyncHttpClient();

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_login);

        requestBtn =(按钮)findViewById(R.id.upload_file);

        PersistentCookieStore myCookieStore =新PersistentCookieStore(本);
        httpClient.setCookieStore(myCookieStore);

        httpClient.setBasicAuth(ApplicationConstants.userName,
                ApplicationConstants.password,新AuthScope(
                        HTTP://*.*.*.*:8080 / someUrl,8080,
                        AuthScope.ANY_REALM));




        requestBtn.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
      httpClient.get(HTTP://*.*.*.*:8080 / someurl,新AsyncHtt presponseHandler(){

                @覆盖
                公共无效的onSuccess(字符串响应){
                的System.out.println(响应);
                Log.d(SUCESSFUL上传,Onsucess+响应);
                }

                @覆盖
                公共无效onFailure(的Throwable为arg0,串ARG1){

                Log.d(LoginActivity,arg0.toString());
                arg0.printStackTrace();
                super.onFailure(为arg0,ARG1);
                }
            });
        }

    }
    });
}
}
 

我得到的,当我点击按钮的异常。它说,验证失败 异常logcat的:

  02-27 16:02:42.930:D / LoginActivity(8869):org.apache.http.client.Htt presponseException:未经授权
02-27 16:02:42.930:W / System.err的(8869):org.apache.http.client.Htt presponseException:未经授权
02-27 16:02:42.930:W / System.err的(8869):在com.loopj.android.http.AsyncHtt$p$psponseHandler.sendResponseMessage(AsyncHtt$p$psponseHandler.java:235)
02-27 16:02:42.930:W / System.err的(8869):在com.loopj.android.http.AsyncHtt prequest.makeRequest(AsyncHtt prequest.java:79)
02-27 16:02:42.930:W / System.err的(8869):在com.loopj.android.http.AsyncHtt prequest.makeRequestWithRetries(AsyncHtt prequest.java:95)
02-27 16:02:42.930:W / System.err的(8869):在com.loopj.android.http.AsyncHtt prequest.run(AsyncHtt prequest.java:57)
02-27 16:02:42.930:W / System.err的(8869):在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:442)
02-27 16:02:42.930:W / System.err的(8869):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
02-27 16:02:42.930:W / System.err的(8869):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-27 16:02:42.940:W / System.err的(8869):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-27 16:02:42.940:W / System.err的(8869):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
02-27 16:02:42.940:W / System.err的(8869):在java.lang.Thread.run(Thread.java:856)
 

有什么错,我在做什么?我使用的URL是正确的。

解决方案

从的基本身份验证与Android

Android的是航运与Apache的HttpClient的4.0 Beta2中,其中有一个缺陷,当它涉及到基本身份验证。

当您搜索的HttpClient和基本身份验证,谷歌肯定会送你到HttpClient的3.X的官方文档,这表明你,怎么办基本验证了的 preemptive方式。这意味着,每个请求发送,而不是等待一个401未经授权响应,然后才发送凭据的客户端的凭据。这可能是要摆在首位什么,因为这样可以节省你的客户端的请求。

  HttpClient的客户端=新的HttpClient();
client.getParams()setAuthentication preemptive(真)。
凭据defaultcreds =新UsernamePasswordCredentials(用户名,密码);
client.getState()setCredentials方法(新AuthScope(myhost的,80,AuthScope.ANY_REALM),defaultcreds)。
 

此示例code不会编译HttpClient的版本4名为setAuthentication preemptive的方法缺失。问题是,如果你忽略这个非常的方法调用中,code仍然有效,但认证并不是preemptive。我们错过了这个小细节,只注意到了一段时间后,即每一个请求是由一个401 未经授权的请求/响应周期pceded $ P $。翻番我们服务的请求的量。

现在检查AsyncHttpClient执行:

 私人最终DefaultHttpClient HttpClient的;
公共无效setBasicAuth(用户字符串,字符串通,AuthScope范围){
    UsernamePasswordCredentials凭证=新UsernamePasswordCredentials(user和pass);
    this.httpClient.getCredentialsProvider()setCredentials方法(适用范围,凭证)。
}
 

所以我认为循环J也可能会遇到这样使用基本身份验证旧的HttpClient 3.x的方式的问题。它的工作,但它不是preemptive。

有一个简单的解决方案将是下载循环J的来源$ C ​​$ c和修改其源代码,并使用修改后的版本。

修改在code将是:

  httpClient.setBasicAuth(ApplicationConstants.userName,
            ApplicationConstants.password);
 

而不是

  httpClient.setBasicAuth(ApplicationConstants.userName,
            ApplicationConstants.password,新AuthScope(
                    的http://*.*.*.*:8080 / uploadservice,80,
                    AuthScope.ANY_REALM));
 

I trying to get authenticated from a website. I am using AsyncHttpClient. Heres the code that I am trying.

Here is my code,

public class LoginActivity extends Activity {

    String tag = "LoginActivity";
    Button requestBtn;
    AsyncHttpClient httpClient = new AsyncHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        requestBtn = (Button) findViewById(R.id.upload_file);

        PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
        httpClient.setCookieStore(myCookieStore);

        httpClient.setBasicAuth(ApplicationConstants.userName,
                ApplicationConstants.password, new AuthScope(
                        "http://*.*.*.*:8080/someUrl", 8080,
                        AuthScope.ANY_REALM));




        requestBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
      httpClient.get("http://*.*.*.*:8080/someurl",new AsyncHttpResponseHandler() {

                @Override
                public void onSuccess(String response) {
                System.out.println(response);
                Log.d("Sucessful upload","Onsucess" + response);
                }

                @Override
                public void onFailure(Throwable arg0,String arg1) {

                Log.d("LoginActivity",arg0.toString());
                arg0.printStackTrace();
                super.onFailure(arg0, arg1);
                }
            });
        }

    }
    });
}
}

I get an exception when I tap on the button. It says authentication failed Exception Logcat:

02-27 16:02:42.930: D/LoginActivity(8869): org.apache.http.client.HttpResponseException: Unauthorized
02-27 16:02:42.930: W/System.err(8869): org.apache.http.client.HttpResponseException: Unauthorized
02-27 16:02:42.930: W/System.err(8869):     at com.loopj.android.http.AsyncHttpResponseHandler.sendResponseMessage(AsyncHttpResponseHandler.java:235)
02-27 16:02:42.930: W/System.err(8869):     at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:79)
02-27 16:02:42.930: W/System.err(8869):     at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:95)
02-27 16:02:42.930: W/System.err(8869):     at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:57)
02-27 16:02:42.930: W/System.err(8869):     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
02-27 16:02:42.930: W/System.err(8869):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-27 16:02:42.930: W/System.err(8869):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-27 16:02:42.940: W/System.err(8869):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-27 16:02:42.940: W/System.err(8869):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-27 16:02:42.940: W/System.err(8869):     at java.lang.Thread.run(Thread.java:856)

What wrong I am doing? The URL I am using is correct.

解决方案

From Basic Authentication with Android:

Android is shipping with Apache's HttpClient 4.0 Beta2, which has a pitfall, when it comes to Basic Authentication.

When you search for HttpClient and Basic Authentication, Google will most definitely send you to the official documentation of HttpClient 3.x, which shows you, how to do Basic Authentication in a preemptive way. That means, sending the client's credentials with every request, instead of waiting for a 401 Unauthorized response and only then sending the credentials. That's probably what you want to in the first place, because it saves your client a request.

HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials("username", "password");
client.getState().setCredentials(new AuthScope("myhost", 80, AuthScope.ANY_REALM), defaultcreds);

This sample code won't compile with HttpClient version 4. The method called setAuthenticationPreemptive is missing. The problem is, if you omit this very method call, the code still works, but the authentication is not preemptive. We missed this little detail and only noticed after a while, that every request was preceded by a 401 Unauthorized request/response cycle. That doubled the amount of requests we served.

Now check the implementation of AsyncHttpClient:

private final DefaultHttpClient httpClient;
public void setBasicAuth( String user, String pass, AuthScope scope){
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user,pass);
    this.httpClient.getCredentialsProvider().setCredentials(scope, credentials);
}

So I think loopj might also run into the problem of using the old HttpClient 3.x way of doing Basic Authentication. It does work, but it's not preemptive.

A straightforward solution will be download the source code of loopj and modify its source and use the modified version.

Modification in code would be:

    httpClient.setBasicAuth(ApplicationConstants.userName,
            ApplicationConstants.password);

instead of

            httpClient.setBasicAuth(ApplicationConstants.userName,
            ApplicationConstants.password, new AuthScope(
                    "http://*.*.*.*:8080/uploadservice", 80,
                    AuthScope.ANY_REALM));

这篇关于AsyncHttpClient验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆