Created
December 2, 2014 12:01
-
-
Save mosladil/787e22d06b8161d4f85a to your computer and use it in GitHub Desktop.
FB Login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cz.osladil.fblogin; | |
import com.gargoylesoftware.htmlunit.WebClient; | |
import com.gargoylesoftware.htmlunit.WebWindow; | |
import com.gargoylesoftware.htmlunit.html.HtmlElement; | |
import com.gargoylesoftware.htmlunit.html.HtmlPage; | |
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput; | |
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; | |
import com.gargoylesoftware.htmlunit.html.HtmlTextInput; | |
import com.gargoylesoftware.htmlunit.util.Cookie; | |
import java.io.IOException; | |
import java.util.Set; | |
public class FbLogin { | |
private static Set<Cookie> facebookLogin(String username, String password) { | |
try { | |
WebClient webClient = new WebClient(); | |
HtmlPage page = webClient.getPage("http://www.facebook.com"); | |
WebWindow window = page.getEnclosingWindow(); | |
HtmlTextInput usernameInput = null; | |
HtmlPasswordInput passwordInput = null; | |
HtmlSubmitInput submitButton = null; | |
for (HtmlElement el: page.getHtmlElementById("login_form").getHtmlElementsByTagName("input")) { | |
if (el instanceof HtmlTextInput) { | |
usernameInput = (HtmlTextInput)el; | |
} | |
if (el instanceof HtmlPasswordInput) { | |
passwordInput = (HtmlPasswordInput)el; | |
} | |
if (el instanceof HtmlSubmitInput) { | |
submitButton = (HtmlSubmitInput)el; | |
} | |
} | |
usernameInput.setValueAttribute(username); | |
passwordInput.setValueAttribute(password); | |
submitButton.click(); | |
if (window.getEnclosedPage().getUrl().toString().contains("welcome")) { | |
return webClient.getCookieManager().getCookies(); | |
} | |
} catch (RuntimeException | IOException ex) { | |
// something went wrong, just fall through | |
} | |
return null; | |
} | |
public static void main(String[] args) throws IOException { | |
Set<Cookie> cookies = facebookLogin("[email protected]", "HentenOny^2"); | |
System.out.println("Login " + (cookies != null ? "succeded." : "failed.")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>cz.osladil</groupId> | |
<artifactId>fblogin</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<maven.compiler.source>1.7</maven.compiler.source> | |
<maven.compiler.target>1.7</maven.compiler.target> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>net.sourceforge.htmlunit</groupId> | |
<artifactId>htmlunit</artifactId> | |
<version>2.15</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment