Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > HTTPClient - Sessions and cookies what am I doing wrong?

Reply
Thread Tools

HTTPClient - Sessions and cookies what am I doing wrong?

 
 
Maverick
Guest
Posts: n/a
 
      02-05-2004
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=QowLx5sE5D3Ix4Kf8ug440635Katv51ho6h9due9 sRcQp2yRP2l5GJOD7zgUuom1

whereas in the second case I get 2 cookies returned back

SessionID=QowLx5sE5D3Ix4Kf8ug440635Katv51ho6h9due9 sRcQp2yRP2l5GJOD7zgUuom1

logins1=dYNh%2FELu%2BpRhzH1IRjBLcrn0Da0Kowdn7sk9Ma 7AX0FnATqZy4PhuA%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.COM PATIBILITY);


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();
}
}
 
Reply With Quote
 
 
 
 
Maverick
Guest
Posts: n/a
 
      02-07-2004
Anyone please?

On Thu, 05 Feb 2004 18:47:14 +0000, Maverick <(E-Mail Removed)> 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=QowLx5sE5D3Ix4Kf8ug440635Katv51ho6h9due 9sRcQp2yRP2l5GJOD7zgUuom1
>
>whereas in the second case I get 2 cookies returned back
>
>SessionID=QowLx5sE5D3Ix4Kf8ug440635Katv51ho6h9due 9sRcQp2yRP2l5GJOD7zgUuom1
>
>logins1=dYNh%2FELu%2BpRhzH1IRjBLcrn0Da0Kowdn7sk9M a7AX0FnATqZy4PhuA%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.COM PATIBILITY);
>
>
> 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();
> }
>}


 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Give Request.Cookies and Response.Cookies is there any reason to use another method to use cookies? _Who ASP .Net 7 09-18-2008 07:49 PM
Apache HttpClient - problem with cookies javateamPL@gmail.com Java 2 02-08-2008 09:28 AM
Cookieless Sessions (Sessions Without Cookies) and Security scottymo ASP .Net Security 3 09-29-2006 11:00 PM
HttpClient forgets Cookies? Snyke Java 0 01-24-2006 11:48 PM
Help with Jakarta HttpClient Cookies Richard Java 0 04-26-2004 09:14 PM



Advertisments