2011-07-15 51 views
14

我在哪裏,我通過server.Initially服務器驗證用戶名/密碼正在實施Basic認證,這樣我的代碼工作正常,但現在服務器已更改爲Digest認證,所以我的舊代碼不能正常工作創建一個Android應用程序。如何在android中使用Digest身份驗證?

應該做怎樣更改使用Digest認證?

我的代碼如下:

private boolean authenticateUser() 

{ 

    try 
    { 
     String url_str = "http://serverweb.com/checkauthentication.php"; 

     HttpPost post = new HttpPost(url_str); 

     Log.v("AUTHENTICATION URL = ", url_str); 
     post.addHeader("Authorization","Basic "+getCredentials()); 
     ResponseHandler<String> responseHandler=new BasicResponseHandler(); 
     String response_body = client.execute(post, responseHandler); 

     Log.v("SERVER RESPONSE DATA = ", response_body); 

     XMLDataParser.parseXML(XMLDataParser.USER_INFORMATION_PARSER_CODE, response_body); 

     List<Cookie> cookies = client.getCookieStore().getCookies(); 
     if (!cookies.isEmpty()) 
     { 
     for (int i = 0; i < cookies.size(); i++) 
     { 
      XMLData.cookie = cookies.get(i); 
     } 
     } 

     return true; 
    } 
    catch (MalformedURLException mue) 
    { 
     Log.i("MalformedURLException", " "+mue.getMessage()); 
     displayDialog("User Does Not exist"); 
     return false; 
    } 
    catch (IOException ioe) 
    { 
     Log.i("IOException", " "+ioe.getMessage()); 
     displayDialog("User Does Not exist"); 
     return false; 
    } 
    catch (Exception e) 
    { 
     Log.i("Exception", " "+e.getMessage()); 
     displayDialog("Error"); 
     return false; 
    } 
} 
private String getCredentials() 
{ 
    String u=edit_username.getText().toString(); 
    String p=edit_password.getText().toString(); 

    Log.v("USER NAME = ",u); 
    Log.v("PASSWORD = ",p); 
    return(Base64.encodeBytes((u+":"+p).getBytes())); 
} 
+0

您的網址不工作 –

+0

@scorpio此URL是不正確的。 – AB1209

回答

15

您需要創建所需的憑據HttpHostHttpContext對象,並給它來執行方法。

這是一個示例代碼,其中您的身份驗證獨立於後端身份驗證。 android的http客戶端將負責將其轉換爲適當的格式。請檢查此示例代碼,這僅供參考,不能直接用於代碼中。 :)

此代碼是在你的活動:

@Override 
public void onResume(){ 
    super.onResume(); 
    AsyncTask<String, Void, Void> httpTask = new TestHttpThread(); 
    httpTask.execute("test_url","test_user","test_password"); 
} 

樣品AsyncActivity

private class TestHttpThread extends AsyncTask<String, Void, Void>{ 

    @Override 
    protected Void doInBackground(String... params) { 
     if(params.length > 0){ 
      String url = params[0]; 
      String username = params[1]; 
      String password = params[2]; 

      try { 
       AndroidHttpClient httpClient = AndroidHttpClient.newInstance("test user agent"); 

       URL urlObj = new URL(url); 
       HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol()); 
       AuthScope scope = new AuthScope(urlObj.getHost(), urlObj.getPort()); 
       UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); 

       CredentialsProvider cp = new BasicCredentialsProvider(); 
       cp.setCredentials(scope, creds); 
       HttpContext credContext = new BasicHttpContext(); 
       credContext.setAttribute(ClientContext.CREDS_PROVIDER, cp); 

       HttpGet job = new HttpGet(url); 
       HttpResponse response = httpClient.execute(host,job,credContext); 
       StatusLine status = response.getStatusLine(); 
       Log.d(TestActivity.TEST_TAG, status.toString()); 
       httpClient.close(); 
      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 

     } 
     return null; 
    } 
} 
+0

使用它我得到例外 後「這個線程禁止HTTP請求」 我能在這種情況下做的請幫助 – AB1209

+0

你必須做在主線程和UI線程此請求。你應該在異步任務或服務中執行代碼 – Naresh

+0

使用新代碼更新代碼..在異步任務中創建http – Naresh

相關問題