AsyncHttpClient身份验证失败

Fah*_*que 2 authentication android android-async-http

我试图通过网站进行身份验证.我在用AsyncHttpClient.下面是我正在尝试的代码.

这是我的代码,

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

    }
    });
}
}
Run Code Online (Sandbox Code Playgroud)

点击按钮时出现异常.它说验证失败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)
Run Code Online (Sandbox Code Playgroud)

我做错了什么?我使用的URL是正确的.

ema*_*olo 12

我想出了另一个解决方案,因为我遇到了代理之间的问题,这些代理不喜欢使用上面提出的解决方案构建Authorization标头setBasicAuth(username, password)以及AsyncHttpClient 的默认(错误)实现.

所以我自己添加了标题,自己做了BASE64用户名/密码编码,但是使用了addHeader()从父客户端实现继承的方法.

它现在有效.

asyncHttpClient.addHeader(
  "Authorization",
    "Basic " + Base64.encodeToString(
      (username+":"+password).getBytes(),Base64.NO_WRAP)
);
Run Code Online (Sandbox Code Playgroud)

我希望这有助于某人.


Sta*_*kER 5

Android的基本身份验证:

Android提供Apache的HttpClient 4.0 Beta2,它在基本身份验证方面存在缺陷.

当您搜索HttpClient和基本身份验证时,Google肯定会将您发送到HttpClient 3.x的官方文档,该文件向您展示了如何以抢占方式进行基本身份验证.这意味着,在每次请求时发送客户端的凭据,而不是等待401 Unauthorized响应,然后才发送凭据.这可能是你想要的,因为它可以为你的客户节省一个请求.

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);
Run Code Online (Sandbox Code Playgroud)

此示例代码不会使用HttpClient版本4进行编译.缺少名为setAuthenticationPreemptive的方法.问题是,如果省略此方法调用,代码仍然有效,但身份验证不是抢占式的.我们错过了这个小细节,只是在一段时间后才注意到,每个请求之前都有一个401 Unauthorized请求/响应周期.这使我们服务的请求数量增加了一倍.

现在检查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);
}
Run Code Online (Sandbox Code Playgroud)

所以我认为loopj也可能遇到使用旧的HttpClient 3.x方式进行基本身份验证的问题.它确实有效,但它不是先发制人的.

一个简单的解决方案是下载loopj的源代码并修改其源代码并使用修改后的版本.

代码中的修改将是:

    httpClient.setBasicAuth(ApplicationConstants.userName,
            ApplicationConstants.password);
Run Code Online (Sandbox Code Playgroud)

代替

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