Trying to do the Gaia Login in Java. I was thinking of using JSoup to fetch all of the names of the inputs, and then fetch the values so that I could then submit them with the username and password to login. I can't figure out how to fetch the name of a randomly-named field though, but it seems like there's some next() thing I could use. I can't be sure though, because I can't check on the JSoup website because it's down.
I already have the username and password submit set up:
But I need to use JSoup to grab the SID and Token and randomly generated input and then submit those too.Code:import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class Login { public static void main(String[] args) throws UnsupportedEncodingException { String lookfor = "from your Gaia account"; String postData = "username=" + URLEncoder.encode("userhere", "UTF-8") + "&password=" + URLEncoder.encode("passhere", "UTF-8"); HTTPRequest request = new HTTPRequest(); request.setURL("http://www.gaiaonline.com.com"); System.out.println("Request: " + request); System.out.println("URL: " + request.getURL()); System.out.println("Get: " + request.get()); System.out.println("Post: " + request.post("/auth/login", postData)); System.out.println("Get: " + request.get()); int index1 = request.get().indexOf(lookfor); if (index1 != -1) { System.out.println("Login Success"); } else { System.out.println("Login Failed"); } } }
I had this as code to try to grab SID a few months ago, but I forget how it worked and it's not producing any output lol:
And I can't check my code and see what it does or anything, because the JSoup website is down.Code:import org.jsoup.Jsoup; import org.jsoup.helper.Validate; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; class JSoupTest { public static void main(String[] args) throws IOException { Document doc = Jsoup.connect("http://gaiaonline.com/auth/login").get(); Elements inputs = doc.select("sid"); for(Element input : inputs) { System.out.println(input.attr("name")); System.out.println(input.attr("value")); } } }
Anyway, thanks.
Results 1 to 5 of 5
- 05 Feb. 2013 05:02pm #1
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 11.26
The JSoup website is down, so does anyone feel like helping me?
- 05 Feb. 2013 11:30pm #2
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
How do I get a substring between tags in a String? - Web Tutorials - avajava.com
There is an example on that page what would allow you get all substrings between two strings. You could do something like that to get the names and values.
- 06 Feb. 2013 12:57am #3
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.25
That's actually like exactly what I need, thanks a ton for that. I'll try to post my Gaia login once I have it finished up.
EDIT: Having a few problems.
This is my source:
Code:import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import org.apache.commons.lang.StringUtils; public class GaiaLogin { public static void main(String[] args) throws UnsupportedEncodingException { String lookfor = "The username or password"; HTTPRequest request = new HTTPRequest(); request.setURL("http://www.gaiaonline.com/auth/login"); System.out.println("URL: " + request.getURL()); String source = request.get(); if (source != null) { System.out.println("Source successfully grabbed: " + source); } else { System.out.println("Error: Couldn't grab page source!"); } String title = StringUtils.substringBetween(source, "<title>", "</title>"); System.out.println("Title: " + title); //Print out page title, for testing purposes String sid = StringUtils.substringBetween(source, "name=\"sid\" value=\"", "\" type=\"hidden\" />"); System.out.println("SID: " + sid); //Print out Session ID String token = StringUtils.substringBetween(source, "name=\"token\" value=\"", "\" />"); System.out.println("Token: " + token); //Print out token value String rname = StringUtils.substringBetween(source, "<input type=\"hidden\" name=\"", "\" value=\""); System.out.println("Random Input Name: " + rname); //Prints out random input name String rvalue = StringUtils.substringBetween(source, "name=\"" + rname + "\" value=\"", "\" />"); System.out.println("Random Input Value: " + rvalue); //prints out random input value } }
Code:URL: http://www.gaiaonline.com/auth/login Source successfully grabbed: *the source was here* Title: Log In | Gaia Online SID: null Token: null Random Input Name: null Random Input Value: null
Last edited by 323; 06 Feb. 2013 at 02:46am.
- 06 Feb. 2013 04:01am #4
Moderator Bachelor of Science in Virginity
- Age
- 31
- Join Date
- Nov. 2009
- Location
- Toronto
- Posts
- 5,421
- Reputation
- 546
- LCash (Rank 3)
- 1.96
My first time touching java in ~3 years.
Code:import java.io.UnsupportedEncodingException; import org.apache.commons.lang.StringUtils; public class GaiaLogin { public static void main(String[] args) throws UnsupportedEncodingException { String lookfor = "The username or password"; HTTPRequest request = new HTTPRequest(); request.setURL("http://www.gaiaonline.com/"); System.out.println("URL: " + request.getURL()); String source = request.get(); if (source != null) { System.out.println("Source successfully grabbed: " + source); } else { System.out.println("Error: Couldn't grab page source!"); } String title = StringUtils.substringBetween(source, "<title>", "</title>"); System.out.println("Title: " + title); //Print out page title, for testing purposes String posts = StringUtils.substringBetween(source, "</fieldset>", "</form>").replace("data-value", "");//limits the string search to the area with the data //posts = posts.replace("data-value", ""); String[] names = StringUtils.substringsBetween(posts, "name=\"", "\"");//finds the names of the strings String[] values = StringUtils.substringsBetween(posts, "value=\"", "\"");//finds the values of the strings System.out.println(names[0] + " => " + values[0]);//prints the first name and value System.out.println(names[1] + " => " + values[1]);//prints the second name and value System.out.println(names[2] + " => " + values[2]);//prints the third name and value System.out.println(names[3] + " => " + values[3]);//prints the fourth name and value //login starts here String user = ""; String pass = ""; String postdata = names[0] + "=" + values[0] + "&" + names[1] + "=" + values[1] + "&" + names[2] + "=" + values[2] + "&" + names[3] + "=" + values[3] + "&" + "&username=" + user + "&password=" + pass; request.setURL("http://www.gaiaonline.com/auth/login/"); String login = request.post("", postdata); if (login.contains("login_success")) { System.out.println("Logged in successfully!"); } else { System.out.println("Login Failed!"); } } }
- 06 Feb. 2013 02:03pm #5
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 0.98