我在嘗試構建一個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」。
我在做什麼錯在這裏?
我不知道你在做什麼錯誤,因爲你沒有告訴我們你執行這段代碼後發生了什麼。你能提供像堆棧跟蹤的任何細節嗎? – home 2012-03-18 17:41:13
噢是啊sry:S nutzername =一個kennwort = a不是一個有效的登錄名。如果我在瀏覽器中這樣做,是顯示消息,該密碼是錯誤的。但我的程序並不打印該消息,但完全相同的文本,如果我用「取得」替換「後」。 – Beginner 2012-03-18 18:02:52
如果這種工作起作用,Postbank會做一個非常糟糕的工作。 – Wolfgang 2012-03-18 18:22:00