HTTPClient - Sessions and cookies what am I doing wrong?

Discussion in 'Java' started by Maverick, Feb 5, 2004.

  1. Maverick

    Maverick Guest

    Hello all,
    I read some good reviews about jakarta HTTPClient about its session
    and cookies management system and fancied giving it a try as a
    learning exercise but somehow I don't seem to be able to get it to
    work properly.
    I'm basically trying to connect to this site
    http://s1.starkingdoms.com/scripts/main.php

    I am able to get past the authentication login page onto the next
    screen but I then can't proceed any further because of Session/Cookies
    issues(the site requires cookies to create user
    sessions).....Basically the server returns back an error message with
    Session not found or cookies disabled, just as you would get if you
    disabled cookies in IE.

    In an attempt to debug, I've tried getting a dump of the cookies that
    my client received from the site using two methos(see code)
    1) cookiespec.match and
    2) client.getState().getCookies()

    In the first case I only get a recognised cookie with containing

    SessionID=QowLx5sE5D3Ix4Kf8ug440635Katv51ho6h9due9sRcQp2yRP2l5GJOD7zgUuom1

    whereas in the second case I get 2 cookies returned back

    SessionID=QowLx5sE5D3Ix4Kf8ug440635Katv51ho6h9due9sRcQp2yRP2l5GJOD7zgUuom1

    logins1=dYNh%2FELu%2BpRhzH1IRjBLcrn0Da0Kowdn7sk9Ma7AX0FnATqZy4PhuA%3D%3D

    Can't really understand why I don't get two cookies returned in te
    first instance and if this is the cause of my problem, but the server
    definitely ins't liking the way the httpclient is handling the
    cookies/session......Could any kind soul out there help me, bearing in
    mind that I'm quite new to all this stuf.......

    MANY THANKS


    import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.cookie.CookiePolicy;
    import org.apache.commons.httpclient.cookie.CookieSpec;
    import org.apache.commons.httpclient.methods.*;

    public class FormLoginDemo
    {
    static final String LOGON_SITE = "s1.starkingdoms.com";
    static final int LOGON_PORT = 80;
    static final String account = "blabla";
    static final String password = "blabla";

    public FormLoginDemo() {
    super();
    }

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

    HttpClient client = new HttpClient();
    client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT,
    "http");
    client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);


    PostMethod authpost = new PostMethod("/scripts/main.php");

    // Prepare login parameters
    NameValuePair[] data = {
    new NameValuePair("account",account),
    new NameValuePair("password",password)
    };

    authpost.setRequestBody(data);

    client.executeMethod(authpost);
    System.out.println("Login form post: " +
    authpost.getStatusLine().toString());

    // release any connection resources used by the method
    authpost.releaseConnection();

    // See if we got any cookies(First Method)
    System.out.println("*************************");
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] initcookies = cookiespec.match(
    LOGON_SITE, LOGON_PORT, "/", false,
    client.getState().getCookies());

    System.out.println("Initial set of cookies:");
    if (initcookies.length == 0) {
    System.out.println("None");
    } else {
    for (int i = 0; i < initcookies.length; i++) {
    System.out.println("- " + initcookies.toString());
    }
    }

    // See if we got any cookies(Second Method)
    System.out.println("*************************");
    for (int i = 0; i < client.getState().getCookies().length;
    i++) {
    System.out.println("- " +
    client.getState().getCookies().toString());
    }
    System.out.println("*************************");

    GetMethod authget = new GetMethod("/scripts/logout.php");

    authget.addRequestHeader("Referer","http://s1.starkingdoms.com/scripts/menu.php");
    authget.addRequestHeader("Connection","Keep-Alive");
    // authget.addRequestHeader("Accept-Encoding","gzip,
    deflate");
    client.executeMethod(authget);

    // See if we got any cookies
    CookieSpec cookiespec2 = CookiePolicy.getDefaultSpec();
    Cookie[] initcookies2 = cookiespec.match(
    LOGON_SITE, LOGON_PORT, "/", false,
    client.getState().getCookies());

    System.out.println("Initial set of cookies:");
    if (initcookies2.length == 0) {
    System.out.println("None");
    } else {
    for (int i = 0; i < initcookies2.length; i++) {
    System.out.println("- " + initcookies2.toString());
    }
    }
    System.out.println("*************************");

    for (int i = 0; i < client.getState().getCookies().length;
    i++) {
    System.out.println("- " +
    client.getState().getCookies().toString());
    }

    System.out.println("*************************");



    System.out.println(new String(authget.getResponseBody()));
    authget.releaseConnection();
    }
    }
    Maverick, Feb 5, 2004
    #1
    1. Advertising

  2. Maverick

    Maverick Guest

    Anyone please?

    On Thu, 05 Feb 2004 18:47:14 +0000, Maverick <> wrote:

    >Hello all,
    >I read some good reviews about jakarta HTTPClient about its session
    >and cookies management system and fancied giving it a try as a
    >learning exercise but somehow I don't seem to be able to get it to
    >work properly.
    >I'm basically trying to connect to this site
    >http://s1.starkingdoms.com/scripts/main.php
    >
    >I am able to get past the authentication login page onto the next
    >screen but I then can't proceed any further because of Session/Cookies
    >issues(the site requires cookies to create user
    >sessions).....Basically the server returns back an error message with
    >Session not found or cookies disabled, just as you would get if you
    >disabled cookies in IE.
    >
    >In an attempt to debug, I've tried getting a dump of the cookies that
    >my client received from the site using two methos(see code)
    >1) cookiespec.match and
    >2) client.getState().getCookies()
    >
    >In the first case I only get a recognised cookie with containing
    >
    >SessionID=QowLx5sE5D3Ix4Kf8ug440635Katv51ho6h9due9sRcQp2yRP2l5GJOD7zgUuom1
    >
    >whereas in the second case I get 2 cookies returned back
    >
    >SessionID=QowLx5sE5D3Ix4Kf8ug440635Katv51ho6h9due9sRcQp2yRP2l5GJOD7zgUuom1
    >
    >logins1=dYNh%2FELu%2BpRhzH1IRjBLcrn0Da0Kowdn7sk9Ma7AX0FnATqZy4PhuA%3D%3D
    >
    >Can't really understand why I don't get two cookies returned in te
    >first instance and if this is the cause of my problem, but the server
    >definitely ins't liking the way the httpclient is handling the
    >cookies/session......Could any kind soul out there help me, bearing in
    >mind that I'm quite new to all this stuf.......
    >
    >MANY THANKS
    >
    >
    >import org.apache.commons.httpclient.*;
    >import org.apache.commons.httpclient.cookie.CookiePolicy;
    >import org.apache.commons.httpclient.cookie.CookieSpec;
    >import org.apache.commons.httpclient.methods.*;
    >
    >public class FormLoginDemo
    >{
    > static final String LOGON_SITE = "s1.starkingdoms.com";
    > static final int LOGON_PORT = 80;
    > static final String account = "blabla";
    > static final String password = "blabla";
    >
    > public FormLoginDemo() {
    > super();
    > }
    >
    > public static void main(String[] args) throws Exception {
    >
    > HttpClient client = new HttpClient();
    > client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT,
    >"http");
    > client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
    >
    >
    > PostMethod authpost = new PostMethod("/scripts/main.php");
    >
    > // Prepare login parameters
    > NameValuePair[] data = {
    > new NameValuePair("account",account),
    > new NameValuePair("password",password)
    > };
    >
    > authpost.setRequestBody(data);
    >
    > client.executeMethod(authpost);
    > System.out.println("Login form post: " +
    >authpost.getStatusLine().toString());
    >
    > // release any connection resources used by the method
    > authpost.releaseConnection();
    >
    > // See if we got any cookies(First Method)
    > System.out.println("*************************");
    > CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    > Cookie[] initcookies = cookiespec.match(
    > LOGON_SITE, LOGON_PORT, "/", false,
    >client.getState().getCookies());
    >
    > System.out.println("Initial set of cookies:");
    > if (initcookies.length == 0) {
    > System.out.println("None");
    > } else {
    > for (int i = 0; i < initcookies.length; i++) {
    > System.out.println("- " + initcookies.toString());
    > }
    > }
    >
    > // See if we got any cookies(Second Method)
    > System.out.println("*************************");
    > for (int i = 0; i < client.getState().getCookies().length;
    >i++) {
    > System.out.println("- " +
    >client.getState().getCookies().toString());
    > }
    > System.out.println("*************************");
    >
    > GetMethod authget = new GetMethod("/scripts/logout.php");
    >
    >authget.addRequestHeader("Referer","http://s1.starkingdoms.com/scripts/menu.php");
    > authget.addRequestHeader("Connection","Keep-Alive");
    > // authget.addRequestHeader("Accept-Encoding","gzip,
    >deflate");
    > client.executeMethod(authget);
    >
    > // See if we got any cookies
    > CookieSpec cookiespec2 = CookiePolicy.getDefaultSpec();
    > Cookie[] initcookies2 = cookiespec.match(
    > LOGON_SITE, LOGON_PORT, "/", false,
    >client.getState().getCookies());
    >
    > System.out.println("Initial set of cookies:");
    > if (initcookies2.length == 0) {
    > System.out.println("None");
    > } else {
    > for (int i = 0; i < initcookies2.length; i++) {
    > System.out.println("- " + initcookies2.toString());
    > }
    > }
    > System.out.println("*************************");
    >
    > for (int i = 0; i < client.getState().getCookies().length;
    >i++) {
    > System.out.println("- " +
    >client.getState().getCookies().toString());
    > }
    >
    > System.out.println("*************************");
    >
    >
    >
    > System.out.println(new String(authget.getResponseBody()));
    > authget.releaseConnection();
    > }
    >}
    Maverick, Feb 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard
    Replies:
    0
    Views:
    686
    Richard
    Apr 26, 2004
  2. Snyke

    HttpClient forgets Cookies?

    Snyke, Jan 24, 2006, in forum: Java
    Replies:
    0
    Views:
    670
    Snyke
    Jan 24, 2006
  3. Replies:
    2
    Views:
    1,795
  4. _Who
    Replies:
    7
    Views:
    2,657
  5. scottymo
    Replies:
    3
    Views:
    693
    Dominick Baier
    Sep 30, 2006
Loading...

Share This Page