• Log in or sign up for an account here: https://www.sythe.org/login

Sythe Login

iJava

.Previously known as RSGoldRush
Joined
Nov 21, 2011
Sythe Gold
491
Messages
1,204
Sythe Login
Ok, So I decided to make a login system which will work with Sythe. This can easily be added to your programs to require them to be a Sythe user to login.

Login.java

Code:
package org.login;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author iJava
 */
public class Login {

    private String username, password, forumPMURL, forumTitile;
    
    public Login(String username, String password, String forumPMURL, String forumTitle) {
        this.username = username;
        this.password = password;
        this.forumPMURL = forumPMURL;
        this.forumTitile = forumTitle;
    }

    public boolean logIn() {
        try {
            WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
            HtmlPage loggedInPage = null;
            HtmlPage sourcePage = webClient.getPage(forumPMURL);
            if (sourcePage.getTitleText().equals(forumTitile)) {
                HtmlTextInput usernameElement = sourcePage.getElementByName("vb_login_username");
                HtmlPasswordInput passwordElement = sourcePage.getElementByName("vb_login_password");
                HtmlElement submitBtnElement = sourcePage.getElementByAccessKey('s');
                if (usernameElement != null && usernameElement.getText().equals("User Name")) {
                    usernameElement.setValueAttribute(username);
                }
                if (passwordElement != null && passwordElement.getText().equals("")) {
                    passwordElement.setValueAttribute(password);
                }
                if (loggedInPage == null && usernameElement != null && passwordElement != null && usernameElement.getText().equals(username) && passwordElement.getText().equals(password)) {
                    try {
                        loggedInPage = submitBtnElement.click();
                    } catch (IOException ex) {
                        Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                if (loggedInPage != null) {
                    return (loggedInPage.asText().contains("Thank you for logging in, " + username));
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FailingHttpStatusCodeException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}

Demo.java

Code:
package org.demo;

import org.login.Login;

/**
 *
 * @author iJava
 */
public class Demo {
    
    public static void main(String[] args0) {
        Login login = new Login("username", "password", "http://www.Sythe.org/private.php?do=newpm&u=1", "Sythe.Org Forums");
        if(login.logIn()) {
            System.out.println("We Logged In!");
        }
    }
}

With a little work this may work with other forums just by changing the values for finding the elements.

You'll need HTMLUnit which is here - http://htmlunit.sourceforge.net/
 
Sythe Login
I was confused on how you got this to work so easily until I noticed the htmlunit stuff.

Also, why not just a static method?
 
Sythe Login
I was confused on how you got this to work so easily until I noticed the htmlunit stuff.

Also, why not just a static method?

Well I was actually going to set it up to work with different forums(software) and may still do so, which is why I made the constructor over static.

HtmlUnit is great :)
 
Runescape Gamblingrunescape gambling
Back
Top