MalformedChallengeException:身份验证质询为空

时间:2014-09-29 04:12:27

标签: java android authentication

我正试图绕过Android中的基本身份验证,并且遇到了这个错误。 请参阅下面的代码:

HttpUriRequest request = new HttpGet(url);
String credentials = "username" + ":" + "password";  
String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);  
request.addHeader("Authorization", "Basic " + base64EncodedCredentials);

HttpClient httpclient = new DefaultHttpClient();  

try {
HttpResponse response = httpclient.execute(request);
Log.d("Login: Response", EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}

这是我得到的错误..

09-29 12:01:21.990: W/System.err(3438): org.apache.http.client.ClientProtocolException
09-29 12:01:21.990: W/System.err(3438):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:672)
09-29 12:01:21.990: W/System.err(3438):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
09-29 12:01:21.990: W/System.err(3438):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-29 12:01:21.990: W/System.err(3438):     at com.accenture.emsmobile.backend.LoginAsyncTask.doInBackground(LoginAsyncTask.java:33)
09-29 12:01:22.000: W/System.err(3438):     at com.accenture.emsmobile.backend.LoginAsyncTask.doInBackground(LoginAsyncTask.java:1)
09-29 12:01:22.000: W/System.err(3438):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-29 12:01:22.000: W/System.err(3438):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-29 12:01:22.000: W/System.err(3438):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-29 12:01:22.000: W/System.err(3438):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-29 12:01:22.000: W/System.err(3438):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-29 12:01:22.010: W/System.err(3438):     at java.lang.Thread.run(Thread.java:841)
09-29 12:01:22.010: W/System.err(3438): Caused by: org.apache.http.auth.MalformedChallengeException: Authentication challenge is empty
09-29 12:01:22.020: W/System.err(3438):     at org.apache.http.impl.auth.RFC2617Scheme.parseChallenge(RFC2617Scheme.java:72)
09-29 12:01:22.020: W/System.err(3438):     at org.apache.http.impl.auth.AuthSchemeBase.processChallenge(AuthSchemeBase.java:111)
09-29 12:01:22.030: W/System.err(3438):     at org.apache.http.impl.auth.BasicScheme.processChallenge(BasicScheme.java:95)
09-29 12:01:22.030: W/System.err(3438):     at org.apache.http.impl.client.DefaultRequestDirector.processChallenges(DefaultRequestDirector.java:1068)
09-29 12:01:22.030: W/System.err(3438):     at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:965)
09-29 12:01:22.030: W/System.err(3438):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:475)
09-29 12:01:22.030: W/System.err(3438):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
09-29 12:01:22.030: W/System.err(3438):     ... 10 more

1 个答案:

答案 0 :(得分:0)

替换:

request.addHeader("Authorization", "Basic " + base64EncodedCredentials);

使用:

request.addHeader("Authorization", "Basic "+Base64.encodeToString("rat#1:rat".getBytes(),Base64.NO_WRAP));