A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

Basic Java Gaia Login

1.7k views · started by 323 ·
#1
Basic Java Gaia Login
A basic Gaia Login written in Java that also dumps all of the cookies, pre and post login.

/*
* Written by Flareboy323 of LogicalGamers.com
* Requires Apache HTTPComponents core and client
* libraries to compile.
*/



import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
* A example that demonstrates how HttpClient APIs can be used to perform
* form-based logon.
*/
public class ClientFormLogin {

public static void main(String[] args) throws Exception {

DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.gaiaonline.com/auth/login");

HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

System.out.println("Login form get: " + response.getStatusLine());
EntityUtils.consume(entity);

System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}

HttpPost httpost = new HttpPost("http://www.gaiaonline.com/auth/login");

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "xxxx"));
nvps.add(new BasicNameValuePair("password", "xxxx"));

httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

response = httpclient.execute(httpost);
entity = response.getEntity();

System.out.println("Login form get: " + response.getStatusLine());
EntityUtils.consume(entity);

System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}

} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}


Edit the xxxx and input your own username and password there.
#2
Doesn't even look like it'd work.

Nice job on the effort though.
#3
Doesn't even look like it'd work.

Nice job on the effort though.


Why do you say that it doesn't look like it would work? I'm adding a comment poster onto it right now, to be completely positive that it logs you in. Know of any better way to make sure that it logs you in?

EDIT: You're right, it doesn't work. I'm getting a 401 Unauthorized when I try to post the comments. Any idea why it's not working? I'm looking at Artificial's and the other one and don't see anything extra
#4
Probably invalid post data in the page form fields.
#5
You're only posting a username and password. Maybe you should consider using an http class rather than doing it all manually.
#7
Artificial wrote:
You're only posting a username and password. Maybe you should consider using an http class rather than doing it all manually.


What do you mean use an http class instead of doing it manually??
#8
What do you mean use an http class instead of doing it manually??


Use a wrapper like the one in my post rather than doing extra manual labor like initializing the http instance, cookie management, generally more coding, etc. The main things that should be exposed to you is what is relevant to you: namely GET/POST requests from the client.
#9
Use a wrapper like the one in my post rather than doing extra manual labor like initializing the http instance, cookie management, generally more coding, etc. The main things that should be exposed to you is what is relevant to you: namely GET/POST requests from the client.


I would love for that, but I couldn't find any HTTP wrappers for Java. The only thing close to it that was easy to use that I found was this one, the Apache HTTP Components thing. I might try out the one you linked me to on Github, it looks promising, but is it fully developed?
#10
I would love for that, but I couldn't find any HTTP wrappers for Java. The only thing close to it that was easy to use that I found was this one, the Apache HTTP Components thing. I might try out the one you linked me to on Github, it looks promising, but is it fully developed?


It should at the very least be capable of simple tasks like GET/POST requests (and of course handles the cookies for you), which is normally all you need. Me or him could always expand on it, or even you could as it is open source, so you can always tailor it to your needs.

If you have any problems w/ it just post away.

Edit: And yeah, we've used it for quite a few projects so it should be pretty stable.
#11
It should at the very least be capable of simple tasks like GET/POST requests (and of course handles the cookies for you), which is normally all you need. Me or him could always expand on it, or even you could as it is open source, so you can always tailor it to your needs.

If you have any problems w/ it just post away.

Edit: And yeah, we've used it for quite a few projects so it should be pretty stable.


Sounds good, I'll try it out. If I expand on it, I'll make sure to post the source either here or github or something. Thanks for the help, by the way.