473,378 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
0 14648

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: windandwaves | last post by:
Hi Gurus I am basically sorry that I have to bother you about this. I am a PHP beginner and I have been studying sessions and cookies over the last few weeks. I have learned lots, but I am...
6
by: Paul | last post by:
Here is a question that should get everyone going. I have an ecommerce site where I need to pass the order_id to every page. So which method is the best practice to pass this variable between...
3
by: Mark | last post by:
I'm told that ram based cookies refer to session cookies (which the browser may still store on disk if it likes). These cookies that are destroyed when the bowser exits. If they are "session...
1
by: mirandacascade | last post by:
Operating system: Windows XP Home Version of Python: 2.4 While looking through the tutorial on ElementSOAP at the following link: http://effbot.org/zone/element-soap.htm I observed sample...
24
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How can I see in javascript if a web browser accepts cookies?...
3
by: sanchita | last post by:
Hello everyone, I didn't get any response in "Security" forum hence posting here again. I am having problem with persistent cookies. Even after setting "CreatePersistentCookie" to true in...
5
by: jheines | last post by:
I am trying to explain how cookies and sessions work in a class I teach, but I have hit a wall when it comes to the interaction between cookies and the state of the privacy settings in Internet...
8
by: Chuck Anderson | last post by:
I've instituted a sessions based scheme on my web site to combat hot linking to my images. When someone requests a page at my site, I set a session variable. I then use htaccess to redirect *all*...
0
by: osa | last post by:
I have a problem with commons-httpclient-3.1. When i try to get some page, cookies do not come to me. There is no error or exception message. Just null text. What could you advise me? Code:...
2
by: StanB | last post by:
I came across this weird problem: 1. Session state stops working after the app is deployed to another server because IE does not accept cookies. 2. It works if cookieless="true" in the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.