HttpComponents 的 HttpClient 对 WordPress 的 Cookie 支持问题

红薯 发布于 2011/12/16 22:21
阅读 2K+
收藏 1

使用 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 呢?

加载中
0
红薯
红薯
找到了解决的办法!!!
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);
}

耶!

返回顶部
顶部