You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by ZelluX <ze...@gmail.com> on 2009/05/13 05:50:18 UTC

How to handle cookies when login?

Hi, all

I have some problem when trying to write a program which automatically
logins into my wordpress administrator panel and do something.

Here's my program:

public class HttpClientTest {
    static HttpClient conn = new HttpClient();
    static String loginURL = "http://my.wordpress.com/wp-login.php";

    public static void main(String[] args) {
        try {
            PostMethod post = new PostMethod(loginURL);
            post.addParameter("log", "username");
            post.addParameter("pwd", "password");
            post.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
            conn.executeMethod(post);
            String redirectLocation = null;
            Header locationHeader = post.getResponseHeader("location");

            System.out.println(post.getResponseBodyAsString());
            if (locationHeader != null) {
                redirectLocation = locationHeader.getValue();
            } else {
                System.out.println(post.getResponseBodyAsString());
            }

            Header[] headers = post.getResponseHeaders();
            for (Header header : headers) {
                System.out.print(header);
            }
            post.setURI(new URI(redirectLocation, true));
            conn.executeMethod(post);

            // store the session info for the next call
            headers = post.getResponseHeaders();
            for (Header header : headers) {
                System.out.print(header);
            }

            Thread.sleep(2000);

            // connect to a page you're interested...
            PostMethod getMethod = new PostMethod("
http://my.wordpress.com/wp-admin/");

            // ...using the session ID retrieved before
            for (Header header : headers) {
                getMethod.setRequestHeader(header);
            }
            conn.executeMethod(post);

            String body = post.getResponseBodyAsString();
            System.out.println(body);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

But when the program runs, it has 'cookie rejected' warnings like the
following:

WARNING: Cookie rejected: "$Version=0;
wordpress_0e90296551ce7c9b5257c1da5aa595bc=zellux%7C1242359071%7C8b824d441be9080e8c1f4d9e98977b7a;
$Path=/wp-content/plugins". Illegal path attribute "/wp-content/plugins".
Path of origin: "/wp-login.php"
May 13, 2009 11:44:30 AM org.apache.commons.httpclient.HttpMethodBase
processCookieHeaders
WARNING: Cookie rejected: "$Version=0;
wordpress_0e90296551ce7c9b5257c1da5aa595bc=zellux%7C1242359071%7C8b824d441be9080e8c1f4d9e98977b7a;
$Path=/wp-admin". Illegal path attribute "/wp-admin". Path of origin:
"/wp-login.php"

And the response header of login page seems to be alright:
Set-Cookie: wordpress_test_cookie=WP+Cookie+check; path=/
Set-Cookie:
wordpress_0e90296551ce7c9b5257c1da5aa595bc=zellux%7C1242359071%7C8b824d441be9080e8c1f4d9e98977b7a;
path=/wp-content/plugins; httponly
Set-Cookie:
wordpress_0e90296551ce7c9b5257c1da5aa595bc=zellux%7C1242359071%7C8b824d441be9080e8c1f4d9e98977b7a;
path=/wp-admin; httponly
Set-Cookie:
wordpress_logged_in_0e90296551ce7c9b5257c1da5aa595bc=zellux%7C1242359071%7C38ff98db7cb98cfb818e7d9894983ac8;
path=/; httponly
Location: http://my.wordpress.com/wp-admin/

But the second request was redirected to
http://my.wordpress.com/wp-login.php?redirect_to=http%3A%2F%2Fmy.wordpress.com%2Fwp-admin%2F

Is there any problem related to cookie handling?
Many thanks for your reply.

Re: How to handle cookies when login?

Posted by GangTae Koh <gt...@embedin.co.kr>.
For Http Components, I used to store cookies like below lines.
---------------------------------------------------------------------------------------------------------------------------------------
   // default HTTP Client
    private static HttpClient httpclient = new BasicHttpContext();
    // Create a local instance of cookie store
    private CookieStore cookieStore = new BasicCookieStore();
    // Create local HTTP context
    private HttpContext httpclient = new DefaultHttpClient();

    // Bind custom cookie store to the local context
     httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

     // Form, userid, passwd
         List<NameValuePair> nvps;

	HttpEntity entity = null;

	HttpGet httpget = new HttpGet(loginUrl);
	HttpResponse response = execute(httpget);
	entity = response.getEntity();
	if (entity != null) {
		entity.consumeContent();
	}

     // process FORM based login with HttpClient.	
	HttpPost httpost = new HttpPost(loginUrl);
	httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
	response = execute( httpost);

	entity = response.getEntity();
	if (entity != null) {
		entity.consumeContent();
	}



2009-05-13 (수), 03:50 +0000, ZelluX:

> Hi, all
> 
> I have some problem when trying to write a program which automatically
> logins into my wordpress administrator panel and do something.
> 
> Here's my program:
> 
> public class HttpClientTest {
>     static HttpClient conn = new HttpClient();
>     static String loginURL = "http://my.wordpress.com/wp-login.php";
> 
>     public static void main(String[] args) {
>         try {
>             PostMethod post = new PostMethod(loginURL);
>             post.addParameter("log", "username");
>             post.addParameter("pwd", "password");
>             post.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
>             conn.executeMethod(post);
>             String redirectLocation = null;
>             Header locationHeader = post.getResponseHeader("location");
> 
>             System.out.println(post.getResponseBodyAsString());
>             if (locationHeader != null) {
>                 redirectLocation = locationHeader.getValue();
>             } else {
>                 System.out.println(post.getResponseBodyAsString());
>             }
> 
>             Header[] headers = post.getResponseHeaders();
>             for (Header header : headers) {
>                 System.out.print(header);
>             }
>             post.setURI(new URI(redirectLocation, true));
>             conn.executeMethod(post);
> 
>             // store the session info for the next call
>             headers = post.getResponseHeaders();
>             for (Header header : headers) {
>                 System.out.print(header);
>             }
> 
>             Thread.sleep(2000);
> 
>             // connect to a page you're interested...
>             PostMethod getMethod = new PostMethod("
> http://my.wordpress.com/wp-admin/");
> 
>             // ...using the session ID retrieved before
>             for (Header header : headers) {
>                 getMethod.setRequestHeader(header);
>             }
>             conn.executeMethod(post);
> 
>             String body = post.getResponseBodyAsString();
>             System.out.println(body);
>         } catch (Exception e) {
>             e.printStackTrace();
>         }
>     }
> }
> 
> But when the program runs, it has 'cookie rejected' warnings like the
> following:
> 
> WARNING: Cookie rejected: "$Version=0;
> wordpress_0e90296551ce7c9b5257c1da5aa595bc=zellux%7C1242359071%7C8b824d441be9080e8c1f4d9e98977b7a;
> $Path=/wp-content/plugins". Illegal path attribute "/wp-content/plugins".
> Path of origin: "/wp-login.php"
> May 13, 2009 11:44:30 AM org.apache.commons.httpclient.HttpMethodBase
> processCookieHeaders
> WARNING: Cookie rejected: "$Version=0;
> wordpress_0e90296551ce7c9b5257c1da5aa595bc=zellux%7C1242359071%7C8b824d441be9080e8c1f4d9e98977b7a;
> $Path=/wp-admin". Illegal path attribute "/wp-admin". Path of origin:
> "/wp-login.php"
> 
> And the response header of login page seems to be alright:
> Set-Cookie: wordpress_test_cookie=WP+Cookie+check; path=/
> Set-Cookie:
> wordpress_0e90296551ce7c9b5257c1da5aa595bc=zellux%7C1242359071%7C8b824d441be9080e8c1f4d9e98977b7a;
> path=/wp-content/plugins; httponly
> Set-Cookie:
> wordpress_0e90296551ce7c9b5257c1da5aa595bc=zellux%7C1242359071%7C8b824d441be9080e8c1f4d9e98977b7a;
> path=/wp-admin; httponly
> Set-Cookie:
> wordpress_logged_in_0e90296551ce7c9b5257c1da5aa595bc=zellux%7C1242359071%7C38ff98db7cb98cfb818e7d9894983ac8;
> path=/; httponly
> Location: http://my.wordpress.com/wp-admin/
> 
> But the second request was redirected to
> http://my.wordpress.com/wp-login.php?redirect_to=http%3A%2F%2Fmy.wordpress.com%2Fwp-admin%2F
> 
> Is there any problem related to cookie handling?
> Many thanks for your reply.