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
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).....B asically 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.matc h and
2) client.getState ().getCookies()
In the first case I only get a recognised cookie with containing
SessionID=QowLx 5sE5D3Ix4Kf8ug4 40635Katv51ho6h 9due9sRcQp2yRP2 l5GJOD7zgUuom1
whereas in the second case I get 2 cookies returned back
SessionID=QowLx 5sE5D3Ix4Kf8ug4 40635Katv51ho6h 9due9sRcQp2yRP2 l5GJOD7zgUuom1
logins1=dYNh%2F ELu%2BpRhzH1IRj BLcrn0Da0Kowdn7 sk9Ma7AX0FnATqZ y4PhuA%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......Co uld any kind soul out there help me, bearing in
mind that I'm quite new to all this stuf.......
MANY THANKS
import org.apache.comm ons.httpclient. *;
import org.apache.comm ons.httpclient. cookie.CookiePo licy;
import org.apache.comm ons.httpclient. cookie.CookieSp ec;
import org.apache.comm ons.httpclient. methods.*;
public class FormLoginDemo
{
static final String LOGON_SITE = "s1.starkingdom s.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.getHostC onfiguration(). setHost(LOGON_S ITE, LOGON_PORT,
"http");
client.getState ().setCookiePol icy(CookiePolic y.COMPATIBILITY );
PostMethod authpost = new PostMethod("/scripts/main.php");
// Prepare login parameters
NameValuePair[] data = {
new NameValuePair(" account",accoun t),
new NameValuePair(" password",passw ord)
};
authpost.setReq uestBody(data);
client.executeM ethod(authpost) ;
System.out.prin tln("Login form post: " +
authpost.getSta tusLine().toStr ing());
// release any connection resources used by the method
authpost.releas eConnection();
// See if we got any cookies(First Method)
System.out.prin tln("********** *************** ");
CookieSpec cookiespec = CookiePolicy.ge tDefaultSpec();
Cookie[] initcookies = cookiespec.matc h(
LOGON_SITE, LOGON_PORT, "/", false,
client.getState ().getCookies() );
System.out.prin tln("Initial set of cookies:");
if (initcookies.le ngth == 0) {
System.out.prin tln("None");
} else {
for (int i = 0; i < initcookies.len gth; i++) {
System.out.prin tln("- " + initcookies[i].toString());
}
}
// See if we got any cookies(Second Method)
System.out.prin tln("********** *************** ");
for (int i = 0; i < client.getState ().getCookies() .length;
i++) {
System.out.prin tln("- " +
client.getState ().getCookies()[i].toString());
}
System.out.prin tln("********** *************** ");
GetMethod authget = new GetMethod("/scripts/logout.php");
authget.addRequ estHeader("Refe rer","http://s1.starkingdoms .com/scripts/menu.php");
authget.addRequ estHeader("Conn ection","Keep-Alive");
// authget.addRequ estHeader("Acce pt-Encoding","gzip ,
deflate");
client.executeM ethod(authget);
// See if we got any cookies
CookieSpec cookiespec2 = CookiePolicy.ge tDefaultSpec();
Cookie[] initcookies2 = cookiespec.matc h(
LOGON_SITE, LOGON_PORT, "/", false,
client.getState ().getCookies() );
System.out.prin tln("Initial set of cookies:");
if (initcookies2.l ength == 0) {
System.out.prin tln("None");
} else {
for (int i = 0; i < initcookies2.le ngth; i++) {
System.out.prin tln("- " + initcookies2[i].toString());
}
}
System.out.prin tln("********** *************** ");
for (int i = 0; i < client.getState ().getCookies() .length;
i++) {
System.out.prin tln("- " +
client.getState ().getCookies()[i].toString());
}
System.out.prin tln("********** *************** ");
System.out.prin tln(new String(authget. getResponseBody ()));
authget.release Connection();
}
}
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
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).....B asically 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.matc h and
2) client.getState ().getCookies()
In the first case I only get a recognised cookie with containing
SessionID=QowLx 5sE5D3Ix4Kf8ug4 40635Katv51ho6h 9due9sRcQp2yRP2 l5GJOD7zgUuom1
whereas in the second case I get 2 cookies returned back
SessionID=QowLx 5sE5D3Ix4Kf8ug4 40635Katv51ho6h 9due9sRcQp2yRP2 l5GJOD7zgUuom1
logins1=dYNh%2F ELu%2BpRhzH1IRj BLcrn0Da0Kowdn7 sk9Ma7AX0FnATqZ y4PhuA%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......Co uld any kind soul out there help me, bearing in
mind that I'm quite new to all this stuf.......
MANY THANKS
import org.apache.comm ons.httpclient. *;
import org.apache.comm ons.httpclient. cookie.CookiePo licy;
import org.apache.comm ons.httpclient. cookie.CookieSp ec;
import org.apache.comm ons.httpclient. methods.*;
public class FormLoginDemo
{
static final String LOGON_SITE = "s1.starkingdom s.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.getHostC onfiguration(). setHost(LOGON_S ITE, LOGON_PORT,
"http");
client.getState ().setCookiePol icy(CookiePolic y.COMPATIBILITY );
PostMethod authpost = new PostMethod("/scripts/main.php");
// Prepare login parameters
NameValuePair[] data = {
new NameValuePair(" account",accoun t),
new NameValuePair(" password",passw ord)
};
authpost.setReq uestBody(data);
client.executeM ethod(authpost) ;
System.out.prin tln("Login form post: " +
authpost.getSta tusLine().toStr ing());
// release any connection resources used by the method
authpost.releas eConnection();
// See if we got any cookies(First Method)
System.out.prin tln("********** *************** ");
CookieSpec cookiespec = CookiePolicy.ge tDefaultSpec();
Cookie[] initcookies = cookiespec.matc h(
LOGON_SITE, LOGON_PORT, "/", false,
client.getState ().getCookies() );
System.out.prin tln("Initial set of cookies:");
if (initcookies.le ngth == 0) {
System.out.prin tln("None");
} else {
for (int i = 0; i < initcookies.len gth; i++) {
System.out.prin tln("- " + initcookies[i].toString());
}
}
// See if we got any cookies(Second Method)
System.out.prin tln("********** *************** ");
for (int i = 0; i < client.getState ().getCookies() .length;
i++) {
System.out.prin tln("- " +
client.getState ().getCookies()[i].toString());
}
System.out.prin tln("********** *************** ");
GetMethod authget = new GetMethod("/scripts/logout.php");
authget.addRequ estHeader("Refe rer","http://s1.starkingdoms .com/scripts/menu.php");
authget.addRequ estHeader("Conn ection","Keep-Alive");
// authget.addRequ estHeader("Acce pt-Encoding","gzip ,
deflate");
client.executeM ethod(authget);
// See if we got any cookies
CookieSpec cookiespec2 = CookiePolicy.ge tDefaultSpec();
Cookie[] initcookies2 = cookiespec.matc h(
LOGON_SITE, LOGON_PORT, "/", false,
client.getState ().getCookies() );
System.out.prin tln("Initial set of cookies:");
if (initcookies2.l ength == 0) {
System.out.prin tln("None");
} else {
for (int i = 0; i < initcookies2.le ngth; i++) {
System.out.prin tln("- " + initcookies2[i].toString());
}
}
System.out.prin tln("********** *************** ");
for (int i = 0; i < client.getState ().getCookies() .length;
i++) {
System.out.prin tln("- " +
client.getState ().getCookies()[i].toString());
}
System.out.prin tln("********** *************** ");
System.out.prin tln(new String(authget. getResponseBody ()));
authget.release Connection();
}
}