2013-01-13 39 views
1

作爲一個小項目的一部分,我正在做的事情永遠不會進入'生產',我需要能夠登錄到一個非常安全的網站和檢索HTML。在java中自動登錄以保護網站?

我一直在尋找這樣做使用Apache公用HTTPCLient。然而,我只是想確保它甚至可能,因爲這個網站是非常安全的,可能有sso方法登錄? 如果可能的話,最好的方法是什麼?一旦我登錄後,我需要能夠導航約三頁,因此需要以某種方式存儲cookie或會話。

非常感謝!

回答

3

是的,它可能使用apache HTTP組件來做到這一點,但是爲了與複雜的網站進行交互,沒有任何東西(我知道)跳動了HtmlUnit。要使用httpcomponents,您需要「編寫」整個http請求序列,如果中間有任何依賴動態內容/ javascript的問題,則會遇到問題。

的HtmlUnit,在另一方面,是一個幾乎完全「盒子裏的加油車」,你可以在腳本更高層次的互動 - 單擊此按鈕,填寫這些值,提交等

+0

偉大的作品,謝謝 – ApiMail

0

你需要jsoup .jar文件發送到特定網站 看看Gmail的表單數據的響應,通過檢查的元素元素verfication

package com.kowthal; 

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.net.CookieHandler; 
import java.net.CookieManager; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.ArrayList; 
import java.util.List; 
import javax.net.ssl.HttpsURLConnection; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class HttpUrlConnectionExample { 

    private List<String> cookies; 
    private HttpsURLConnection conn; 

    private final String USER_AGENT = "Mozilla/5.0"; 

    public static void main(String[] args) throws Exception { 

    String url = "https://accounts.google.com/ServiceLoginAuth"; 
    String gmail = "https://mail.google.com/mail/"; 

    HttpUrlConnectionExample http = new HttpUrlConnectionExample(); 

    // make sure cookies is turn on 
    CookieHandler.setDefault(new CookieManager()); 

    // 1. Send a "GET" request, so that you can extract the form's data. 
    String page = http.GetPageContent(url); 
    String postParams = http.getFormParams(page, "[email protected]", "password"); 

    // 2. Construct above post's content and then send a POST request for 
    // authentication 
    http.sendPost(url, postParams); 

    // 3. success then go to gmail. 
    String result = http.GetPageContent(gmail); 
    System.out.println(result); 
    } 

    private void sendPost(String url, String postParams) throws Exception { 

    URL obj = new URL(url); 
    conn = (HttpsURLConnection) obj.openConnection(); 

    // Acts like a browser 
    conn.setUseCaches(false); 
    conn.setRequestMethod("POST"); 
    conn.setRequestProperty("Host", "accounts.google.com"); 
    conn.setRequestProperty("User-Agent", USER_AGENT); 
    conn.setRequestProperty("Accept", 
     "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 
    for (String cookie : this.cookies) { 
     conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]); 
    } 
    conn.setRequestProperty("Connection", "keep-alive"); 
    conn.setRequestProperty("Referer", "https://accounts.google.com/ServiceLoginAuth"); 
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    conn.setRequestProperty("Content-Length", Integer.toString(postParams.length())); 

    conn.setDoOutput(true); 
    conn.setDoInput(true); 

    // Send post request 
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); 
    wr.writeBytes(postParams); 
    wr.flush(); 
    wr.close(); 

    int responseCode = conn.getResponseCode(); 
    System.out.println("\nSending 'POST' request to URL : " + url); 
    System.out.println("Post parameters : " + postParams); 
    System.out.println("Response Code : " + responseCode); 

    BufferedReader in = 
      new BufferedReader(new InputStreamReader(conn.getInputStream())); 
    String inputLine; 
    StringBuffer response = new StringBuffer(); 

    while ((inputLine = in.readLine()) != null) { 
     response.append(inputLine); 
    } 
    in.close(); 
    // System.out.println(response.toString()); 

    } 

    private String GetPageContent(String url) throws Exception { 

    URL obj = new URL(url); 
    conn = (HttpsURLConnection) obj.openConnection(); 

    // default is GET 
    conn.setRequestMethod("GET"); 

    conn.setUseCaches(false); 

    // act like a browser 
    conn.setRequestProperty("User-Agent", USER_AGENT); 
    conn.setRequestProperty("Accept", 
     "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 
    if (cookies != null) { 
     for (String cookie : this.cookies) { 
      conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]); 
     } 
    } 
    int responseCode = conn.getResponseCode(); 
    System.out.println("\nSending 'GET' request to URL : " + url); 
    System.out.println("Response Code : " + responseCode); 

    BufferedReader in = 
      new BufferedReader(new InputStreamReader(conn.getInputStream())); 
    String inputLine; 
    StringBuffer response = new StringBuffer(); 

    while ((inputLine = in.readLine()) != null) { 
     response.append(inputLine); 
    } 
    in.close(); 

    // Get the response cookies 
    setCookies(conn.getHeaderFields().get("Set-Cookie")); 

    return response.toString(); 

    } 

    public String getFormParams(String html, String username, String password) 
     throws UnsupportedEncodingException { 

    System.out.println("Extracting form's data..."); 

    Document doc = Jsoup.parse(html); 

    // Google form id 
    Element loginform = doc.getElementById("gaia_loginform"); 
    Elements inputElements = loginform.getElementsByTag("input"); 
    List<String> paramList = new ArrayList<String>(); 
    for (Element inputElement : inputElements) { 
     String key = inputElement.attr("name"); 
     String value = inputElement.attr("value"); 

     if (key.equals("Email")) 
      value = username; 
     else if (key.equals("Passwd")) 
      value = password; 
     paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8")); 
    } 

    // build parameters list 
    StringBuilder result = new StringBuilder(); 
    for (String param : paramList) { 
     if (result.length() == 0) { 
      result.append(param); 
     } else { 
      result.append("&" + param); 
     } 
    } 
    return result.toString(); 
    } 

    public List<String> getCookies() { 
    return cookies; 
    } 

    public void setCookies(List<String> cookies) { 
    this.cookies = cookies; 
    } 

}