9

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("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();
    }

}

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("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();
tree-hacker
  • 5,351
  • 9
  • 38
  • 39
  • But i'm getting some exceptions like Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/protocol/ProtocolSocketFactory. Can you specify which version of Htmlunit jar is to be downloaded just to make sure that I did the right thing – lulu Apr 24 '14 at 15:43

2 Answers2

13

There are some problems in your code

  1. is that login_form is not the form name but the form ID
  2. the submit button value i Log In
  3. type of password field is HtmlPasswordInput

so:

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();
Luca
  • 331
  • 2
  • 7
12

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("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

Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
  • 1
    Thanks for the answer. I've tried the code (see update above) however I get an element not found exception for the login form for some reason? Any idea why? – tree-hacker Feb 18 '10 at 00:00
  • It's the first form on the page so you should be able to get it via the getForms()[0] method. You might have trouble submitting the form, but I'm sure it's possible via HttpUnit. – Jonathan Holloway Feb 18 '10 at 06:53
  • 1
    And how do you get returned data, like session token, with this? – User May 06 '12 at 20:42
  • it is showing some sort of exceptions when i used the above code...Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[form] attributeName=[name] attributeValue=[UIFullPage_Container] what the error in my code..Plz help me – lulu Apr 24 '14 at 16:10
  • I'm getting error "Cannot resolve symbol 'WebClient'", how to use this with pom.xml ? – vikramvi Sep 01 '22 at 07:42
  • This doesn't work in 2022, not able to login to https://www.valueresearchonline.com/login, using above solution – vikramvi Sep 01 '22 at 11:41