我正在尝试编写一个Java程序,可以自动登录Facebook。
到目前为止,我已经编写了以下代码,可以将主页HTML页面下载到字符串中,但是不知道如何发送电子邮件和密码以登录Facebook?此外,Java程序是否需要处理返回的cookie以保持登录状态?
有人知道这是为什么吗?
到目前为止,我已经编写了以下代码,可以将主页HTML页面下载到字符串中,但是不知道如何发送电子邮件和密码以登录Facebook?此外,Java程序是否需要处理返回的cookie以保持登录状态?
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.facebook.com/");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc
.getInputStream()));
String inputLine;
String allInput = "";
while ((inputLine = in.readLine()) != null) {
allInput += inputLine + "\r\n";
}
System.out.println(allInput);
in.close();
}
更新:
我已经尝试使用htmlUnit编写下面的代码,但是我得到了以下异常:
Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[form] attributeName=[name] attributeValue=[login_form] at com.gargoylesoftware.htmlunit.html.HtmlPage.getFormByName(HtmlPage.java:588)
有人知道这是为什么吗?
final WebClient webClient = new WebClient();
final HtmlPage page1 = webClient.getPage("http://www.facebook.com");
final HtmlForm form = page1.getFormByName("login_form");
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Login").get(0);
final HtmlTextInput textField = form.getInputByName("email");
textField.setValueAttribute("jon@jon.com");
final HtmlTextInput textField2 = form.getInputByName("pass");
textField2.setValueAttribute("ahhhh");
final HtmlPage page2 = button.click();