2012-03-18 149 views
-1

我在嘗試構建一個Java程序,該程序使用https協議自動登錄到網站。我只是想輸入我的名字和密碼到我的編,點擊提交,並得到將出現的頁面,如果我在瀏覽器中。用java登錄HTTPS網站

我已經寫了一個程序,顯示使用SSLSocket類的網站的html代碼。

import java.io.PrintWriter; 
import java.io.UnsupportedEncodingException; 
import java.util.Scanner; 

import javax.net.ssl.SSLSocket; 
import javax.net.ssl.SSLSocketFactory; 

public class bankpost{ 
    public static void main(String[] args) throws UnsupportedEncodingException { 
    final String page = "banking.postbank.de"; 
    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 
    try { 
     SSLSocket s = (SSLSocket) sf.createSocket(page, 443); 

     Scanner in = new Scanner(s.getInputStream()); 
     PrintWriter out = new PrintWriter(s.getOutputStream(), true); 

     System.out.println("Sending request to server " 
       + s.getInetAddress()); 
     out.print("POST /rai/login HTTP1.1\r\n\r\n"); 
     String content = "nutzername=a&kennwort=a"; 
     String cmd = "POST /rai/login HTTP/1.1\n" 
       + "Host: banking.postbank.de\n" 
       + "Content-Type: application/x-www-form-urlencoded\n" 
       + "Content-Length: " + content.length() + "\n\n" 
       + content + "\r\n\r\n"; 
     out.print(cmd); 
     out.flush(); 
     in.useDelimiter(">"); 
     System.out.println("Reading..."); 
     int len = 0; 
     while (in.hasNext()) { 
      String line = in.next(); 
      len += line.length(); 
      if (line.contains("field-input") || line.contains("nicht")) 
       System.err.println(line.trim()); 
      else 
       System.out.println(line.trim()); 
     } 

     System.out.println("DONE - " + len); 
     System.out.print(cmd); 
     in.close(); 
     out.close(); 
     s.close(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
} 

...我要輸入的字段是「nutzername」和「kennwort」。

我在做什麼錯在這裏?

+1

我不知道你在做什麼錯誤,因爲你沒有告訴我們你執行這段代碼後發生了什麼。你能提供像堆棧跟蹤的任何細節嗎? – home 2012-03-18 17:41:13

+0

噢是啊sry:S nutzername =一個kennwort = a不是一個有效的登錄名。如果我在瀏覽器中這樣做,是顯示消息,該密碼是錯誤的。但我的程序並不打印該消息,但完全相同的文本,如果我用「取得」替換「後」。 – Beginner 2012-03-18 18:02:52

+0

如果這種工作起作用,Postbank會做一個非常糟糕的工作。 – Wolfgang 2012-03-18 18:22:00

回答

0

我會嘗試一些標準的HttpsConnection魔術。 SSL需要證書和信任管理器才能正確設置。初始化HttpsContext這樣的:

SSLContext sc = SSLContext.getInstance("SSL"); 
    // here you may want to call sc.init() with your key managers and trustmanagers 
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
    HttpsURLConnection.setDefaultHostnameVerifier(verifier); 
    HttpsURLConnection.setFollowRedirects(false); 

    //setting authenticator which will push your credentials to the server when required 
    Authenticator.setDefault(new Authenticator() { 
     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(config.getUserName(), config.getPassword().toCharArray()); 
     } 
    }) 

,其中驗證是descentant類實現的HostnameVerifier接口,並接受你正在使用的網站的網址。

上下文完成後,您只需使用新的URL()。getConnection()就可以使用任何其他簡單的http站點。

+0

有沒有不同的方式沒有使用HttpsURLConnection? – Beginner 2012-03-18 19:58:35