2013-07-30 124 views
-1

我正在製作一個android應用程序,它使用編碼的JSON從網頁中檢索數據。在我進入網頁瀏覽器的頁面時,它要求我輸入我的用戶名和密碼進行服務器身份驗證。我如何做到這一點,當我做一個httpPost;它會發送一個用戶名和密碼,所以我可以訪問該頁面。Android httpClient服務器身份驗證

它的這種身份認證http://www.wpwhitesecurity.com/wp-content/uploads/2012/08/web-server-authentication-dialogue-box.png

的我該怎麼辦呢?這裏是我的代碼:

  DefaultHttpClient httpClient = new DefaultHttpClient(); 


     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = null; 
     try { 
      httpResponse = httpClient.execute(httpPost); 
     } catch (ClientProtocolException e2) { 
      // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } catch (IOException e2) { 
      // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     try { 
      is = httpEntity.getContent(); 
     } catch (IllegalStateException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
+0

你想用戶名和密碼到服務器與url..isn't它? –

+0

它的這種服務器用戶名和通過http://www.wpwhitesecurity.com/wp-content/uploads/2012/08/web-server-authentication-dialogue-box.png –

回答

0

您可以像這樣把你的用戶名和密碼:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair(your_username_key_string, your_username)); 
nameValuePairs.add(new BasicNameValuePair(your_password_key_string, your_password));    

HttpPost oHttpPost = new HttpPost(your_server_url); 
oHttpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
+0

your_username_key_string和your_username之間的區別是什麼?我不知道第一個是什麼? –

+0

儘管NameValuePair是一個鍵 - 值對,那麼your_username_key_string是字符串格式的關鍵字,而your_username是該鍵的字符串格式的值,您要發佈 –

0

好......這樣你就可以通過它:

DefaultHttpClient httpClient = new DefaultHttpClient(); 

String url = "yoururl"+"username="+YourUsername+"password="+Your Paasword; 

    HttpPost httpPost = new HttpPost(url); 

    HttpResponse httpResponse = null; 
    try { 
     httpResponse = httpClient.execute(httpPost); 
    } catch (ClientProtocolException e2) { 
     // TODO Auto-generated catch block 
     e2.printStackTrace(); 
    } catch (IOException e2) { 
     // TODO Auto-generated catch block 
     e2.printStackTrace(); 
    } 
    HttpEntity httpEntity = httpResponse.getEntity(); 
    try { 
     is = httpEntity.getContent(); 
    } catch (IllegalStateException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
0

添加參數到帖子:

final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("username", username)); 
nameValuePairs.add(new BasicNameValuePair("password", password)); 
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 

這是assu你的登錄表單看起來像:

input type="text" name="username" 
input type="password" name="password" 
+0

它的服務器用戶名和密碼就像這個http://www.wpwhitesecurity。 COM /可溼性粉劑內容/上傳/ 2012/08/Web服務器的身份驗證 - 對話 - box.png –