login with cookies using Apache httpClient
Hello everybody,
I just was thinking about how to realize an automatic login with cookie authentification. I found some bibs from Apache: httpClient
I used this tutotial to figure that out:
Java Tips - How to use HTTP cookies
Authentication works fine, so I get a valid cookie back to my HttpClient instance. But unfortunately if I want to get the actual content page I always get the error of an expired session.
The login works with an SSL secured page. Rest is not secured.
This is my example code:
Code:
// Get HTTP client instance
HttpClient httpclient = new HttpClient();
httpclient.getHttpConnectionManager().
getParams().setConnectionTimeout(30000);
httpclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
// POST URL
String postURL = "https://secure.thisisjustapage.com/start.php";
PostMethod httppost = new PostMethod(postURL);
httppost.addParameter("loginName", "mylogin");
httppost.addParameter("pass", "mypass");
try{
httpclient.executeMethod(httppost);
System.out.println(httppost.getStatusLine());
}
catch(Exception e)
{
e.printStackTrace();
}
httppost.releaseConnection();
// Now, I have my valid cookies
// GET URL
String strURL = "http://www.thisisjustapage.com/start.php";
// Get HTTP GET method
GetMethod httpget = new GetMethod(strURL);
int result = 0;
try{
// Execute HTTP GET
result = httpclient.executeMethod(httpget);
}
catch(Exception e)
{
e.printStackTrace();
}
// Display status code
System.out.println("Response status code: " + result);
System.out.println(httpget.getResponseBodyAsString());
// Release current connection to the connection pool
// once you are done
httpget.releaseConnection();
}
Can anybody help me?
Thanks
Mac