By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,062 Members | 1,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,062 IT Pros & Developers. It's quick & easy.

HTTPClient Cookies and Session- What am I doing wrong??

P: n/a
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();
}
}
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.