如何使用Java编程自动登录Facebook?

9
我正在尝试编写一个Java程序,可以自动登录Facebook。
到目前为止,我已经编写了以下代码,可以将主页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();

但是我遇到了一些异常,例如“主线程中的异常”java.lang.NoClassDefFoundError: org/apache/commons/httpclient/protocol/ProtocolSocketFactory。您能否指定要下载哪个版本的Htmlunit jar,以确保我做了正确的事情? - lulu
2个回答

13

你的代码存在一些问题:

  1. login_form 不是表单名称,而是表单 ID。
  2. 提交按钮的值应该为 Log In
  3. 密码字段的类型应该是 HtmlPasswordInput

因此:

final WebClient webClient = new WebClient();
final HtmlPage page1 = webClient.getPage("http://www.facebook.com");
final HtmlForm form = (HtmlForm) page1.getElementById("login_form");

final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Log In").get(0);
final HtmlTextInput textField = form.getInputByName("email");
textField.setValueAttribute("jon@jon.com");
final HtmlPasswordInput textField2 = form.getInputByName("pass");
textField2.setValueAttribute("ahhhh");
final HtmlPage page2 = button.click();

1
感谢您提供的代码,它帮助我解决了早期版本中的错误。 - Anoop Isaac

12

你应该看一下HTMLUnit,它比使用上面的方法要简单得多。以下页面和代码应该会指导你:

final WebClient webClient = new WebClient();
final HtmlPage page1 = webClient.getPage("http://www.facebook.com");
final HtmlForm form = page1.getFormByName("login_form");

final HtmlSubmitInput button = form.getInputsByValue("Log in");
final HtmlTextInput textField = form.getInputByName("email");
textField.setValueAttribute("jon@jon.com");
final HtmlTextInput textField = form.getInputByName("pass");
textField.setValueAttribute("ahhhh");
final HtmlPage page2 = button.click();

http://htmlunit.sourceforge.net/gettingStarted.html


1
感谢您的回答。我已经尝试了代码(请参见上面的更新),但是出现了一个元素未找到的异常,不知道为什么会这样?您有什么想法吗? - tree-hacker
这是页面上的第一个表单,所以您应该能够通过getForms()[0]方法获取它。您可能会在提交表单时遇到麻烦,但我相信可以通过HttpUnit实现。 - Jonathan Holloway
1
那么,你如何使用它来获取返回的数据,例如会话令牌? - User
当我使用上述代码时,它显示了某种异常...主线程中的异常com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[form] attributeName=[name] attributeValue=[UIFullPage_Container]我的代码出了什么问题..请帮帮我。 - lulu
我遇到了“无法解析符号'WebClient'”的错误,如何在pom.xml中使用它? - vikramvi
这个解决方案在2022年不起作用,无法登录https://www.valueresearchonline.com/login。 - vikramvi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接