Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
992 views
in Technique[技术] by (71.8m points)

apache httpclient 4.x - Java HtmlUnit - can't login to wordpress

I'm trying to use HtmlUnit to login to my local wordpress website but it seems to have a cookies issue.

That's that begining of the code:

WebClient webClient = new WebClient();
HtmlPage loginPage = webClient.getPage("http://localhost/flowersWp/wp-admin");
HtmlForm form = loginPage.getFormByName("loginform");

That's what I get in the log. Anyone has an idea? Thanks.

Nov 27, 2010 12:43:35 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies WARNING: Cookie rejected: "[version: 0][name: wordpress_2418eeb845ebfb96f6f1a71ab8c5625a][value: +][domain: localhost][path: /flowersWp/wp-admin][expiry: Fri Nov 27 12:43:35 IST 2009]". Illegal path attribute "/flowersWp/wp-admin". Path of origin: "/flowersWp/wp-login.php"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

WebClient is using apache httpclient, so it is an HttpClient problem.

From my experience, it has to do with redirections. I've gotten rid of this problem using HttpClient and registering my own cookie support:

  // Create a local instance of cookie store
  CookieStore cookieStore = new BasicCookieStore();

  // Bind custom cookie store to the local context
  httpclient.setCookieStore(cookieStore);
  CookieSpecFactory csf = new CookieSpecFactory() {
      public CookieSpec newInstance(HttpParams params) {
          return new BrowserCompatSpec() {
              @Override
              public void validate(Cookie cookie, CookieOrigin origin)
              throws MalformedCookieException {
                // Oh, I am easy.
                // Allow all cookies
                log.debug("custom validate");
              }
          };
      }
  };
  httpclient.getCookieSpecs().register("easy", csf);
  httpclient.getParams().setParameter(
       ClientPNames.COOKIE_POLICY, "easy"); 

Well, in HtmlUnit I have no direct access to httpclient, but I'm thinking of changing its source code to do so, as I need to connect to wordpress with JavaScript support.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...