Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2004

Tip: Looking for answers? Try searching our database.

HTTPClient - Sessions and cookies what am I doing wrong?

Thread view: 
Maverick - 05 Feb 2004 18:47 GMT
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[i].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()[i].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[i].toString());
           }
       }
       System.out.println("*************************");
       
       for (int i = 0; i < client.getState().getCookies().length;
i++) {
           System.out.println("- " +
client.getState().getCookies()[i].toString());    
       }
 
       System.out.println("*************************");

             
       
       System.out.println(new String(authget.getResponseBody()));
        authget.releaseConnection();
   }
}
Maverick - 07 Feb 2004 14:25 GMT
Anyone please?

>Hello all,
>I read some good reviews about jakarta HTTPClient about its session
[quoted text clipped - 138 lines]
>    }
>}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.