如何使用Java编程登录Facebook?
我正在尝试编写一个Java程序,可以自动登录Facebook 到目前为止,我已经获得了以下代码,可以将主页下载成字符串,但不知道如何发送电子邮件和密码以登录Facebook?Java程序是否还需要处理返回的cookie以保持登录状态如何使用Java编程登录Facebook?,java,html,facebook,cookies,post,Java,Html,Facebook,Cookies,Post,我正在尝试编写一个Java程序,可以自动登录Facebook 到目前为止,我已经获得了以下代码,可以将主页下载成字符串,但不知道如何发送电子邮件和密码以登录Facebook?Java程序是否还需要处理返回的cookie以保持登录状态 public static void main(String[] args) throws Exception { URL url = new URL("http://www.facebook.com/"); URLConnectio
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();
您应该看看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();
您的代码中有一些问题
login\u表单
不是表单名称而是表单ID登录
HtmlPasswordInput
谢谢你的回答。我已经尝试了代码(参见上面的更新),但是出于某种原因,我得到了登录表单的元素not found异常?知道为什么吗?它是页面上的第一个表单,因此您应该能够通过getForms()[0]方法获取它。您可能在提交表单时遇到问题,但我相信可以通过HttpUnit实现。您如何使用此功能获取返回的数据,如会话令牌?当我使用上述代码时,它显示了某种异常…线程“main”com.gargoylesoftware.htmlunit.ElementNotFoundException中的异常:elementName=[form]attributeName=[name]attributeValue=[UIFullPage_Container]我的代码中有什么错误..请帮助我储存代码。。帮助我解决了早期VeriSon的错误,但我遇到了一些异常,比如线程“main”java.lang.NoClassDefFoundError:org/apache/commons/httpclient/ProtocolSocketFactory中的异常。你能指定下载哪个版本的Htmlunit jar来确保我做了正确的事情吗
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();