HttpClient - logs in, then loses cookie
I've been banging my head against a wall over this for the past couple of days and I just can't work it out so I'm praying someone can help me with it.
Basically I'm logging into a WordPress account, I post the login details and get a cookie back which shows I'm logged in, I then try to access the user maintenance panel and I get redirected back to the logon page. When I cycle through my cookies it seems my logon cookie has gone. I'm even using the new v4 HttpClient, which supposedly has better cookie management.
If anyone has any idea of what is going on, I'd really appreciate it.
Here is a fully functional example:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
public class FormLoginDemo
{
public static void main(String args[])
{
try
{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.articlepub.com/wp-login.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
EntityUtils.consume(entity);
}
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (Cookie cooky : cookies) {
System.out.println("- " + cooky.toString());
}
}
HttpPost httpost = new HttpPost("http://www.articlepub.com/wp-login.php");
List <NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("log", "***********"));
nvps.add(new BasicNameValuePair("pwd", "***********"));
nvps.add(new BasicNameValuePair("rememberme", "forever"));
nvps.add(new BasicNameValuePair("redirect_to", "http://some-wp-site.com/wp-admin/"));
nvps.add(new BasicNameValuePair("testcookie", "1"));
nvps.add(new BasicNameValuePair("wp-submit", "Log In"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
EntityUtils.consume(entity);
}
System.out.println("Post logon cookies:");
开发者_运维百科 cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (Cookie cooky : cookies) {
System.out.println("- " + cooky.toString());
}
}
HttpGet httpGet = new HttpGet("http://www.articlepub.com/wp-admin/");
response = httpclient.execute(httpGet);
entity = response.getEntity();
System.out.println("Page Contents: " + EntityUtils.toString(entity));
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
EntityUtils.consume(entity);
}
System.out.println("Post get cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (Cookie cooky : cookies) {
System.out.println("- " + cooky.toString());
}
}
httpclient.getConnectionManager().shutdown();
} catch (Exception e) {
System.out.println(e);
}
}
}
You need to set httpclient to process cookies, for example:
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
精彩评论