我想从 Java exe 编写自动登录到表单的代码,这将允许我点击一个href链接,该链接将重定向到另一个站点,该站点将有一个登录名,然后我将提供凭据,然后下载内容页面,然后在页面中输入详细信息并提交。
这对于 Java 编码来说听起来是不是一项艰巨的任务?通常这个过程是由人工操作员完成的,但我需要自动化它。
我想从 Java exe 编写自动登录到表单的代码,这将允许我点击一个href链接,该链接将重定向到另一个站点,该站点将有一个登录名,然后我将提供凭据,然后下载内容页面,然后在页面中输入详细信息并提交。
这对于 Java 编码来说听起来是不是一项艰巨的任务?通常这个过程是由人工操作员完成的,但我需要自动化它。
1.- Prepare a Sniffer like HttpFox Firefox add-on or Whireshark.
2.- Do login in the site and take the url of the login, the method ( GET or POST ) and the parameters name for username and password ( You may also get this from the source code of the login page ).
3.- Navigate into the site and take de name of the session cookies ( or from all the cookies ).
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class Tests {
public static String cookie;
public static void main(String[] args) throws MalformedURLException, IOException {
URL url = new URL("https://some_site...");
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setRequestMethod( "POST" ); //or GET
https.addRequestProperty( "usename_field_name", "username" );
https.addRequestProperty( "password_field_name", "password" );
https.addRequestProperty( "Cookie", cookie );
BufferedReader in = new BufferedReader(new InputStreamReader(https.getInputStream()));
String linea;
while ((linea = in.readLine()) != null) {
// Recover the cookie and save it in a variable.
// You must put it in other connections.
}
}
}