到目前为止,我已经下载了以下代码,将家庭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?
发布评论