0

当我添加授权标头时,android 会引发 filenotfound 异常。如果我删除

con.setRequestProperty ("Authorization", basicAuth);

一切都很好?

con = (HttpsURLConnection)url.openConnection();
            String userpass = userNameEditText.getText() + ":" +  passwordEditText.getText();

            String basicAuth = "Basic " + Base64.encodeToString(userpass.getBytes(), Base64.NO_WRAP);

            con.setRequestProperty ("Authorization", basicAuth);
4

2 回答 2

1

我没有验证它为什么起作用,但请尝试执行以下操作

http://developer.android.com/reference/java/net/HttpURLConnection.html使用以下代码

Authenticator.setDefault(new Authenticator() {
     protected PasswordAuthentication getPasswordAuthentication() {
       return new PasswordAuthentication(username, password.toCharArray());

   });
 }

完成后,无需添加 Authorization 标头,因为此类会自动为您添加它。

我有一个应用程序仅在某些设备上出现异常,并且将您的代码更改为此代码为我在所有设备上修复它。

我希望它会有所帮助

于 2014-03-27T18:10:35.490 回答
0

我使用了以下内容,它就像一个魅力。

//Base64 basic http authentication
String textToBeEncoded = mName+":"+mPassword;
byte[] data = null;
data = textToBeEncoded.getBytes("UTF-8");
encoding = Base64.encodeToString(data,Base64.NO_WRAP);
Log.d(TAG,"encoded string: "+encoding);
//--------------------------------
HttpGet httpRequest = new HttpGet("//REQUEST URL//");
httpRequest.addHeader("Authorization", "Basic "+encoding);
HttpClient httpclient = new DefaultHttpClient();
response = httpclient.execute(httpRequest);

尝试改用这种方式..

希望它有效。

于 2012-12-22T16:18:53.297 回答