当前位置:  开发笔记 > 编程语言 > 正文

如何使用Java以编程方式登录Facebook?

如何解决《如何使用Java以编程方式登录Facebook?》经验,为你挑选了2个好方法。

我正在尝试编写一个可以自动登录Facebook的Java程序.

到目前为止我有以下代码将home html页面下载到String中但不知道如何发送电子邮件和密码登录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();

小智.. 13

您的代码中存在一些问题

    login_form不是表单名称,而是表单ID

    提交按钮值i Log In

    密码字段的类型是 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();


Jon.. 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> 小智..:

您的代码中存在一些问题

    login_form不是表单名称,而是表单ID

    提交按钮值i Log In

    密码字段的类型是 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();



2> Jon..:

你应该看看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

推荐阅读
雯颜哥_135
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有