2012-03-18 152 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