2012-01-30 23 views
1

下面的程序不斷輸出原始登錄頁面的源代碼在POINT B.爲什麼會發生這種情況?我怎樣才能解決這個問題?想要下一個網頁的數據應該出現。試圖登錄到網站...變得有點困惑

<form method="POST" name="logonform" action="/check-443/logon.logonform" autocomplete="off"> 
     <input type="hidden" name="CDT"     value="12313"> 
     <input type="hidden" name="NEW_PASSWORD"   value=""> 
     <tr> 
     <td class="content"> 
      <label for="User">Username</label> 
     </td> 
     <td> 
      <input type="text" name="LOGON" id="id" value="" size="15" maxlength="15"> 
     </td> 
     </tr> 
     <tr> 
     <td class="content"> 
      <label for="pass">Password:</label> 
     </td> 
     <td> 
      <input type="password" name="PASSWORDS" id="pw" value="" size="15"> 
     </td> 
     </tr> 
     <tr> 
     <td> 
      <input type="submit" value="Log In"> 
     </td> 
     </form> 

我的代碼似乎沒有工作(我看到這個網上,並認爲這可能是適用)

HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("https://abc.cba.com"); 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("id", "Username")); 
    nameValuePairs.add(new BasicNameValuePair("pw", "*****")); 
    nameValuePairs.add(new BasicNameValuePair("Log In", "Log In")); 
    try { 
     UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nameValuePairs); 
     httppost.setEntity(p_entity); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     int status_code = response.getStatusLine().getStatusCode(); 

     if (status_code >= 300) { 
      Log.d("asf", "Error"); //POINT A*************** 
      return false; 

     } else { 
      Log.d("asf", EntityUtils.toString(entity)); //POINT B*************** 
      return true; 
     } 

    } catch (Exception uee){} 
    return false; 
+1

@Chris:謝謝你打掃這個貼子。我不知道如何發佈。 – bluejamesbond 2012-01-30 08:40:08

回答

1

爲了使用您的客戶端而不是HTML登錄頁面進行「登錄」,您需要將登錄表單數據發佈到表單所在的同一位置。

在這種情況下,您應該發佈到客戶端的「/check-443/logon.logonform」,以便像Web表單那樣工作(這是當前表單發佈數據的地方)。

我建議去w3schools,並刷新你對錶格的理解。

+0

謝謝youuuuuuuu – bluejamesbond 2012-02-01 09:28:37

+0

我有一個對話框出現後登錄..... ughhhhh ..我可以甚至去做點擊確定。瞭解更多關於 – bluejamesbond 2012-02-01 09:47:59

+0

一個對話框意味着你正在瀏覽器中使用JavaScript打開'AlertDialog',如果你只是通過響應檢索HTML,將不會出現對話框。 – Graeme 2012-02-01 09:55:29

0

嘗試增加那些隱藏的輸入字段(CDT,NEW_PASSWORD),以namevaluepairs中,看看是否有什麼區別。不記得如果「登錄」需要在那裏,可能會嘗試刪除。

+0

我把字段ID或名稱? – bluejamesbond 2012-01-30 16:29:00

+0

這是發生了什麼stackoverflow.com/questions/8853070/interpreting-http-post-response – bluejamesbond 2012-01-30 16:34:38

+0

你應該使用「name」屬性。你似乎在使用「ID」。之前沒有注意到,但將其切換爲「名稱」。 – gelupa 2012-01-31 05:53:28