2012-10-09 33 views
0

我有一個可運行的代碼,在Froyo上一切正常。但在ICS上,它表示它確實連接,但給我一個文件未找到錯誤,並返回-1作爲文件大小。HttpURLConnection的身份驗證器在Froyo中工作,但不是IceCreamSandwich

對於不需要驗證的文件,ICS沒有問題。

有沒有不同的方式來在ICS上進行身份驗證?或者我錯過了一些ICS HttpURLConnection的細節?

Authenticator.setDefault(new Authenticator() { 
protected PasswordAuthentication getPasswordAuthentication() { 
return new PasswordAuthentication(user,pass); 
        } 
       }); 
URL url = new URL(URLString); 

HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
c.setRequestMethod("GET"); 
c.setDoOutput(true); 
c.connect();//connection says that it is connected 
final int filesize = c.getContentLength();//file size is -1 when using ICS 
c.disconnect(); 

而且我無法驗證到Froyo的一個HTTPS URL,這就是此刻雖然是次要的..

感謝,如果你能幫助..

我一直走了一段時間,但這是我現在使用的。它不使用ICS的工作,我不是在此刻與Froyo的測試,從而不能說肯定它是否與這兩個作品...

private void setAuthenticationCredentials(final String username, final String password) { 
      Authenticator.setDefault(new Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(username, password 
          .toCharArray()); 

回答

0

我一直在使用HttpURLConnection.setRequestProperty()這樣驗證我的要求:

String unencoded = username + ":" + password; 
String encoded = Base64.encodeToString(unencoded.getBytes(), Base64.DEFAULT); 
Log.d(getClass().getName(), "encoded: " + encoded); 
// Attach as authentication header. 
connection.setRequestProperty("Authorization", "Basic " + encoded); 

但是,這似乎有相反的問題。它適用於3.0及更高版本,但不適用於2.3.4及更低版本(服務器明顯發送400,儘管請求不在服務器日誌中)。請告訴我,如果你得到這個工作/已經得到它的工作,我會很感激的信息。

+1

我加了我當前的方法到問題的末尾。 – Kickaha

+0

謝謝。我現在也使用'Authenticator',我沒有看到任何問題。這絕對是做到這一點的正確方法。 –

相關問題