...

View Full Version : Help with logging into a website with java



nolanator19
04-20-2012, 08:21 AM
So I am working on a capstone for my University that requires me to log in to a website running on tomcat with a java program. When my browser logs in it uses this javascript function to actually send the post. When the browser logs in it gets back a new cookie to use but my java program doesn't get one and is just returned to the log in page. If you could look in to the code and see if I am not handling things like cookies correctly would be awesome.



function ajax_login()
{
$( '#login_button' ).bind( 'click', function()
{
var username = $( '#username' ).val();
var password = $( '#password' ).val();

$.post( '../dhis-web-commons-security/login.action', {
'j_username' : username,
'j_password' : password
} ).success( function()
{
var ret = dhis2.availability.syncCheckAvailability();

if ( !ret )
{
alert( i18n_ajax_login_failed );
}
} );
} );
}


I am trying to emulate the javascript with some java using Apache's LoginClientForm example but I have been so far unsuccessful.



public class ClientFormLogin {

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

DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://192.168.1.126:8080/dhis/dummy");

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://192.168.1.126:8080/dhis/dhis-web-commons/security/login.action;jsessionid=" + cookies.get(0).getValue());;

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("j_username", "admin"));
nvps.add(new BasicNameValuePair("j_password", "district"));

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

response = httpclient.execute(httpost);
entity = response.getEntity();
InputStream f = response.getEntity().getContent();
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());
}
}
HttpGet httpget2 = new HttpGet("http://192.168.1.126:8080/dhis/dummy;jsessionid=" + cookies.get(0).getValue());
HttpResponse response2 = httpclient.execute(httpget2);
InputStream i = response2.getEntity().getContent();
EntityUtils.consume(response2.getEntity());


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


I also have some packet traces that I could also post of my browser successfully logging in and the java program failing to log in if it would help. Any insight as to why the server rejects my login would be great. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum