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

编程入门 行业动态 更新时间:2024-10-25 10:21:01
本文介绍了如何使用Java以编程方式登录到Facebook?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试编写一个可以自动登录Facebook的Java程序。

到目前为止,我已经下载了以下代码,将家庭html页面下载到一个字符串,但不知道如何发送电子邮件和密码登录到Facebook? Java程序还需要处理返回的Cookie以保持登录状态?

public static void main(String [] args)throws例外{ URL url = new URL(www.facebook/); URLConnection yc = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(yc .getInputStream())); String inputLine; String allInput =; while((inputLine = in.readLine())!= null){ allInput + = inputLine +\r\\\; } System.out.println(allInput); in.close(); }

}

更新:

我使用htmlUnit尝试了以下代码,但是我收到以下异常:

线程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(www.facebook); 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); final HtmlTextInput textField2 = form.getInputByName(pass); textField2.setValueAttribute(ahhhh); final HtmlPage page2 = button.click();

解决方案

你应该看看HTMLUnit,比使用上述要简单得多。以下页面和代码应指导您:

final WebClient webClient = new WebClient(); final HtmlPage page1 = webClient.getPage(www.facebook); final HtmlForm form = page1.getFormByName(login_form); final HtmlSubmitInput button = form.getInputsByValue(登录); final HtmlTextInput textField = form.getInputByName(email); textField.setValueAttribute(jon@jon); final HtmlTextInput textField = form.getInputByName(pass); textField.setValueAttribute(ahhhh); final HtmlPage page2 = button.click();

htmlunit.sourceforge/gettingStarted.html

I'm trying to write a Java program that can automatically log into Facebook.

I've got the below code so far that downloads the home html page into a String but don't know how to send the email and password to log into Facebook? Also will the Java program need to handle cookies returned to remain logged in?

public static void main(String[] args) throws Exception { URL url = new URL("www.facebook/"); 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(); }

}

Update:

I've tried the below code using htmlUnit however I get the following exception:

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)

Anyone know why this is?

final WebClient webClient = new WebClient(); final HtmlPage page1 = webClient.getPage("www.facebook"); 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"); final HtmlTextInput textField2 = form.getInputByName("pass"); textField2.setValueAttribute("ahhhh"); final HtmlPage page2 = button.click();

解决方案

You should take a look at HTMLUnit, it'll be much simpler than using the above. The following page and code should guide you:

final WebClient webClient = new WebClient(); final HtmlPage page1 = webClient.getPage("www.facebook"); final HtmlForm form = page1.getFormByName("login_form"); final HtmlSubmitInput button = form.getInputsByValue("Log in"); final HtmlTextInput textField = form.getInputByName("email"); textField.setValueAttribute("jon@jon"); final HtmlTextInput textField = form.getInputByName("pass"); textField.setValueAttribute("ahhhh"); final HtmlPage page2 = button.click();

htmlunit.sourceforge/gettingStarted.html

更多推荐

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

本文发布于:2023-11-28 13:25:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   方式   Java   Facebook

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!