2011-10-12 48 views
1

我一直在試圖訪問一個網站,爲Android應用程序,我發展分析數據,但我有沒有運氣,當談到在記錄登錄後解析HTML源。與Java

的網站

<form action="/mobile/login" method="post"> 
    <input type="hidden" name="login_security_token" value="b22155c7259f402f8e005a771c460670">  
    <input type="hidden" name="redirect" value="/mobile">  
    <input type="hidden" name="p_next_page" value="">  


    <input name="nickname" maxlength="25" type="text" value="" />    
    <input name="password" type="password" value="" /> 

    <button name="step" type="submit" value="Login">Login</button> 
</form> 

任何人都可以請建議我如何使用Java然後解析重定向頁面登錄到這個網站:https://giffgaff.com/mobile/login

而下方則是從該網頁(HTML)剝離出來的形式的版本?

到現在爲止,我已經試過上的線流程:

public static void main(Context context) { 
    try { 
     // Construct data 
     String data = URLEncoder.encode("nickname", "UTF-8") + "=" + URLEncoder.encode("testingA", "UTF-8"); 
     data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode("testing", "UTF-8"); 

     // Send data 
     URL url = new URL("https://giffgaff.com/mobile/login"); 
     URLConnection conn = url.openConnection(); 
     conn.setDoOutput(true); 
     OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
     wr.write(data); 
     wr.flush(); 

     // Get the response 
     BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     String str = ""; 
     String line; 
     while ((line = rd.readLine()) != null) { 
      str += line; 
     } 

     AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 
     alertDialog.setTitle("Output"); 
     alertDialog.setMessage(str); 
     alertDialog.setButton("Okay", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }); 
     alertDialog.show(); 

     wr.close(); 
     rd.close(); 
    } catch (Exception e) { 
     AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 
     alertDialog.setTitle("ERROR"); 
     alertDialog.setMessage(e.toString()); 
     alertDialog.setButton("Okay", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }); 

     alertDialog.show(); 
    } 
} 

但我嘗試返回頁面如果登錄信息是不正確的。

如果你想看到自己的登錄頁面的行爲,這裏的一些測試登錄詳細信息: 暱稱(用戶名):testingA 密碼:測試 該網站還似乎依賴於一個名爲「napaSessionId」

曲奇

回答

2

首先一個忠告,如果你沒有直接的權限要做到這一點,要小心,有問題的網站可能會在他們的服務條款排除這一點。

要回答這個問題,有很多原因很多網站會拒絕登錄。要做到這一點,你需要儘可能接近瀏覽器如何處理交易。要做到這一點,你需要看看真正的瀏覽器在做什麼。

HTTPS是更棘手,因爲很多HTTP嗅探器無法處理它,但HttpWatch的要求就可以了。檢查HTTP事務,然後嘗試複製它們。

您的url.openConnection()調用實際上會返回一個HTTPURLConnction的實例,並將其轉換爲&,然後您將可以輕鬆設置各種http頭,例如User-Agent。

最後一點,你說可能需要一個cookie。你的代碼不會處理cookie。爲此,您需要使用Cookie管理器,例如:http://download.oracle.com/javase/tutorial/networking/cookies/index.html

0

您可能想要查看Jsoup,htmlUnithttpUnit。現在我想這個權利,我面臨着各種各樣的困難,但我敢肯定,這些項目之一,是要走的路...

祝你好運,讓我貼!