Comment se connecter à Facebook par programmation en utilisant Java?
J'essaie d'écrire un programme Java qui peut se connecter automatiquement à Facebook.
J'ai jusqu'à présent le code ci-dessous qui télécharge la page html d'accueil dans une chaîne mais je ne sais pas comment envoyer l'e-mail et le mot de passe pour me connecter à Facebook? Le programme Java devra-t-il également gérer les cookies retournés pour rester connecté?
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();
}
}
mise à Jour:
J'ai essayé le code ci-dessous en utilisant htmlUnit mais j'obtiens l'exception suivante:
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)
Quelqu'un savoir pourquoi il en est?
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("[email protected]");
final HtmlTextInput textField2 = form.getInputByName("pass");
textField2.setValueAttribute("ahhhh");
final HtmlPage page2 = button.click();
9
Author: tree-hacker, 2010-02-18
2 answers
Vous devriez jeter un oeil à HTMLUnit, ce sera beaucoup plus simple que d'utiliser ce qui précède. La page et le code suivants devraient vous guider:
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("[email protected]");
final HtmlTextInput textField = form.getInputByName("pass");
textField.setValueAttribute("ahhhh");
final HtmlPage page2 = button.click();
12
Author: Jon, 2010-02-17 23:39:37
Il y a quelques problèmes dans votre code
- que
login_form
n'est pas le nom du formulaire, mais l'ID du formulaire - la valeur du bouton soumettre i
Log In
- le type de champ de mot de passe est
HtmlPasswordInput
Donc:
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("[email protected]");
final HtmlPasswordInput textField2 = form.getInputByName("pass");
textField2.setValueAttribute("ahhhh");
final HtmlPage page2 = button.click();
13
Author: Luca, 2012-02-29 07:52:17