HTTPClient - Sessions and cookies what am I doing wrong?

M

Maverick

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();
}
}
 
M

Maverick

Anyone please?

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();
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top