Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-20-2012, 08:21 AM   PM User | #1
nolanator19
New to the CF scene

 
Join Date: Apr 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
nolanator19 is an unknown quantity at this point
Help with logging into a website with java

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.

Code:
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.

Code:
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.

Last edited by nolanator19; 04-20-2012 at 09:45 AM..
nolanator19 is offline   Reply With Quote
Reply

Bookmarks

Tags
apache, java, login, tomcat

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:16 AM.


Advertisement
Log in to turn off these ads.