开发者社区> 问答> 正文

HttpClient 对 WordPress 的 Cookie 支持问题 - 服务报错

"

使用 HttpComponents 的 HttpClient 包来访问 WordPress 的登录页面,有如下的警告信息

2011-12-16 22:15:37 org.apache.http.client.protocol.ResponseProcessCookies processCookies警告: Cookie rejected: "[version: 0][name: wordpress_0f6656bfca67cbc6623cf00ad16ac695][value: ld%7C1324217627%7C291f724c73c12859ae871c3b51bea61a][domain: blog.makingware.com][path: /wp-content/plugins][expiry: null]". Illegal path attribute "/wp-content/plugins". Path of origin: "/"

2011-12-16 22:15:37 org.apache.http.client.protocol.ResponseProcessCookies processCookies

警告: Cookie rejected: "[version: 0][name: wordpress_0f6656bfca67cbc6623cf00ad16ac695][value: ld%7C1324217627%7C291f724c73c12859ae871c3b51bea61a][domain: blog.makingware.com][path: /wp-admin][expiry: null]". Illegal path attribute "/wp-admin". Path of origin: "/"

代码如下:

private final static DefaultHttpClient client = new DefaultHttpClient();
static {
	client.getParams().setParameter(CookieSpecPNames.SINGLE_COOKIE_HEADER, true);
	client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
}

试遍了 CookiePolicy 中的所有常量值都无效,除了那个 IGNORE_COOKIES

好像这是一个bug,我用的是 4.1.2 版本,这是 HttpClient 最新的 GA 版本。

还有什么其他办法能让 HttpComponents 接受 WordPress 的 Cookie 呢?

"

展开
收起
montos 2020-05-31 23:12:44 553 0
1 条回答
写回答
取消 提交回答
  • "找到了解决的办法!!!
    <pre class=""brush:java; toolbar: true; auto-links: false;"">private final static DefaultHttpClient client = new DefaultHttpClient(); static { CookieStore cookieStore = new BasicCookieStore(); //Bind custom cookie store to the local context client.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"); } }; } }; client.getCookieSpecs().register("oschina", csf); client.getParams().setParameter(ClientPNames.COOKIE_POLICY, "oschina"); client.getParams().setParameter(CookieSpecPNames.SINGLE_COOKIE_HEADER, true); }

    耶!

    ######<img src=""http://www.oschina.net/js/ke/plugins/emoticons/0.gif"" alt="""" />
    "

    2020-05-31 23:12:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载