2011-04-21 62 views
2

我試圖登錄到使用基於表單的身份驗證的站點,以便我的應用程序可以進入,下載受保護的頁面,然後退出(是,我有一個有效的用戶名/密碼組合)。Java - 登錄到使用基於表單的身份驗證的網站

我知道:
1.網址登錄頁面
2.鏈接直接登錄認證
3.方法(POST)
4.我的信息(顯然)
5.用戶名和密碼字段(根據...改變,我已經寫了一個方法來獲取名稱)。

目前我使用 this dream.in.code page的代碼作爲我的努力的基礎。

每次運行該應用程序時,它都會返回帶有「錯誤的用戶名/密碼」消息的登錄頁面。

代碼:

import java.net.*; 
import java.util.LinkedList; 
import java.io.*; 

import javax.swing.JOptionPane; 

public class ConnectToURL 
{ 

    // Variables to hold the URL object and its connection to that URL. 
    private static URL URLObj; 
    private static URLConnection connect; 
    private static String loginField; 
    private static String passwordField; 

    private static void getFields() 
    { 
    try 
    { 
     URLObj = new URL("http://url.goes.here/login.jsp"); 
     connect = URLObj.openConnection(); 
     // Now establish a buffered reader to read the URLConnection's input 
     // stream. 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
      connect.getInputStream())); 

     String lineRead = ""; 
     LinkedList<String> lines = new LinkedList<String>(); 

     // Read all available lines of data from the URL and print them to 
     // screen. 
     while ((lineRead = reader.readLine()) != null) 
     { 
     lines.add(lineRead); 
     } 
     reader.close(); 

     while(lines.peekFirst().indexOf("<th>Username or E-mail:</th>") == -1) 
     { 
     lines.removeFirst(); 
     } 
     String usernameCell = ""; 
     while (usernameCell.indexOf("</td>") == -1) 
     { 
     usernameCell = usernameCell + lines.removeFirst().trim(); 
     } 
     usernameCell = usernameCell.substring(usernameCell.indexOf("name=\"") + 6); 
     usernameCell = usernameCell.substring(0, usernameCell.indexOf("\"")); 
     loginField = usernameCell; 

     while(lines.peekFirst().indexOf("<th>Password:</th>") == -1) 
     { 
     lines.removeFirst(); 
     } 

     String passwordCell = ""; 
     while (passwordCell.indexOf("</td>") == -1) 
     { 
     passwordCell = passwordCell + lines.removeFirst().trim(); 
     } 
     passwordCell = passwordCell.substring(passwordCell.indexOf("name=\"") + 6); 
     passwordCell = passwordCell.substring(0, passwordCell.indexOf("\"")); 
     passwordField = passwordCell; 
    } 
    catch (MalformedURLException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

    public static void main(String[] args) 
    { 
    try 
    { 
     // getFields() grabs the names of the username and password fields and stores them into variables above 
     getFields(); 
     // Establish a URL and open a connection to it. Set it to output 
     // mode. 
     URLObj = new URL("http://url.goes.here/login_submit.jsp"); 
     connect = URLObj.openConnection(); 
     HttpURLConnection.setFollowRedirects(true); 
     connect.setDoOutput(true); 
    } 
    catch (MalformedURLException ex) 
    { 
     System.out 
      .println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again."); 
     System.exit(1); 
    } 
    catch (Exception ex) 
    { 
     System.out.println("An exception occurred. " + ex.getMessage()); 
     System.exit(1); 
    } 

    try 
    { 
     // Create a buffered writer to the URLConnection's output stream and 
     // write our forms parameters. 
     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
      connect.getOutputStream())); 
     // For obvious reasons, login info is editted. 
     // The line begins with username=& because there's a username field that send no data and is set to display:none. 
     // When I observed the request in Chrome, username was sent, but left blank. Without it, my request doesn't go through. 
     writer.write("username=&" + loginField + "=" + URLEncoder.encode("Username", "UTF-8") + "&" + passwordField + "=" + URLEncoder.encode("myPassword", "UTF-8")); 
     writer.close(); 

     // Now establish a buffered reader to read the URLConnection's input 
     // stream. 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
      connect.getInputStream())); 

     String lineRead = ""; 

     // Read all available lines of data from the URL and print them to 
     // screen. 
     while ((lineRead = reader.readLine()) != null) 
     { 
     System.out.println(lineRead); 
     } 

     reader.close(); 
    } 
    catch (Exception ex) 
    { 
     System.out.println("There was an error reading or writing to the URL: " 
      + ex.getMessage()); 
    } 
    } 
} 

回答

0

我會嘗試使用類似HttpFoxFiddler看到究竟正在登錄時發送的是什麼,並試圖模仿這一點。有時登錄頁面會按照Javascript發送的內容。

+0

我試過,並沒有做任何事。我試着模仿我在Chrome開發人員工具中看到的所有內容,這讓我無處可去。 – Caleb 2011-04-21 19:51:18

0

使用LiveHTTPHeaders檢出所有發佈的東西。您可能沒有通過POST命令傳遞cookie/session數據。

此外,引薦有時監測並應該通過使報頭僞造以及「推薦人:http://homepage.com.../login.html

+0

我試圖給它正確的推薦人,它讓我無處可去。我嘗試着從一個新的瀏覽器會話直接進入登錄頁面,並且也能夠登錄。 – Caleb 2011-04-21 19:53:00

0

你嘗試使用Appache的HttpClient(http://hc.apache.org/httpcomponents-client- ga /)寧願寫你自己的代碼,你解析HTML和插入值?

我相信,你不必解析html。你的步驟應該是

  • 查看HTML,看看到「網址」你的身份驗證請求將
  • 打開你發現而發送到「登錄頁」的網址的連接,並將其解析至找。
  • httpclient類可以幫助您管理會話以保持會話的活躍。你可以自己做,但它會是很多工作
+0

您的第1步是在我的代碼中處理的,就像您的第2步一樣。至於第3步,我一定會考慮它。 – Caleb 2011-04-21 20:41:07

相關問題