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.

Reply via email to