Java HtmlUnit - 无法登录 WordPress

发布于 2024-10-05 05:42:46 字数 703 浏览 0 评论 0原文

我正在尝试使用 HtmlUnit 登录我的本地 WordPress 网站,但似乎存在 Cookie 问题。

这就是代码的开头:

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

这就是我在日志中得到的内容。有人有主意吗? 谢谢。

2010 年 11 月 27 日 12:43:35 下午 org.apache.http.client.protocol.ResponseProcessCookies processCookies 警告:Cookie 被拒绝:“[版本:0][名称: wordpress_2418eeb845ebfb96f6f1a71ab8c5625a][值:+][域: 本地主机][路径:/flowersWp/wp-admin][到期时间:11 月 27 日星期五 12:43:35 IST 2009]”。非法路径属性“/flowersWp/wp-admin”。来源路径: “/flowersWp/wp-login.php”

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛上了 2024-10-12 05:42:46

WebClient使用的是apache httpclient,所以这是一个HttpClient问题。

根据我的经验,这与重定向有关。我已经使用 HttpClient 并注册了自己的 cookie 支持摆脱了这个问题:

  // 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"); 

嗯,在 HtmlUnit 中我无法直接访问 httpclient,但我正在考虑更改其源代码来这样做,因为我需要连接到 wordpress具有 JavaScript 支持。

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.

扛起拖把扫天下 2024-10-12 05:42:46

我必须注意,在 HttpClient 4+ 中,我必须执行以下操作:

        CookieSpecProvider csf = new CookieSpecProvider() {
            @Override
            public CookieSpec create(HttpContext context)
            {
                return new BrowserCompatSpec() {
                    @Override
                    public void validate(Cookie cookie, CookieOrigin origin)
                        throws MalformedCookieException
                    {
                        // Allow all cookies
                    }
                };
            }
        };

        RequestConfig requestConfig = RequestConfig.custom()
            .setCookieSpec("easy")
            .build();

        httpclient = HttpClients
            .custom()
//          .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
            .setDefaultCookieStore(cookieStore)
            .setDefaultCookieSpecRegistry(RegistryBuilder.<CookieSpecProvider>create()
                                              .register(CookieSpecs.BEST_MATCH, csf)
                                              .register(CookieSpecs.BROWSER_COMPATIBILITY, csf)
                                              .register("easy", csf).build())
            .setDefaultRequestConfig(requestConfig)
//          .setSSLSocketFactory(sslsf)
            .build();

I must note that in HttpClient 4+, I had to do the following:

        CookieSpecProvider csf = new CookieSpecProvider() {
            @Override
            public CookieSpec create(HttpContext context)
            {
                return new BrowserCompatSpec() {
                    @Override
                    public void validate(Cookie cookie, CookieOrigin origin)
                        throws MalformedCookieException
                    {
                        // Allow all cookies
                    }
                };
            }
        };

        RequestConfig requestConfig = RequestConfig.custom()
            .setCookieSpec("easy")
            .build();

        httpclient = HttpClients
            .custom()
//          .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
            .setDefaultCookieStore(cookieStore)
            .setDefaultCookieSpecRegistry(RegistryBuilder.<CookieSpecProvider>create()
                                              .register(CookieSpecs.BEST_MATCH, csf)
                                              .register(CookieSpecs.BROWSER_COMPATIBILITY, csf)
                                              .register("easy", csf).build())
            .setDefaultRequestConfig(requestConfig)
//          .setSSLSocketFactory(sslsf)
            .build();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文