Sythe Login

Discussion in 'Programming General' started by iJava, Aug 10, 2012.

Sythe Login
  1. Unread #1 - Aug 10, 2012 at 4:23 PM
  2. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,184
    Referrals:
    9
    Sythe Gold:
    464
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    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/
     
  3. Unread #2 - Aug 10, 2012 at 5:06 PM
  4. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    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?
     
  5. Unread #3 - Aug 11, 2012 at 2:35 AM
  6. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,184
    Referrals:
    9
    Sythe Gold:
    464
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Sythe Login

    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 :)
     
  7. Unread #4 - Aug 27, 2012 at 12:32 PM
  8. Aintaro
    Joined:
    Jan 12, 2010
    Posts:
    45
    Referrals:
    0
    Sythe Gold:
    9

    Aintaro Member

    Sythe Login

    Oh nice work there man.
     
  9. Unread #5 - Mar 8, 2021 at 11:06 PM
  10. faryad ali chattha
    Joined:
    Mar 5, 2021
    Posts:
    58
    Referrals:
    0
    Sythe Gold:
    57
    Vouch Thread:
    Click Here

    faryad ali chattha Member

    Sythe Login

    Thanks a lot
     
< Looking for C# Developer | >

Users viewing this thread
1 guest


 
 
Adblock breaks this site