我正在构建一个 Android 应用程序,它使用 httpclient 将数据发布到 WordPress 服务器并检索数据。
由于 cookie 中的路径无效,我无法发送发布数据。这是我检索到的日志:
Cookie rejected: "BasicClientCookie[version=0,name=wordpress_654732f696815924ebd07fb96f161421,domain=[my-domain],path=/wp-content/plugins,expiry=Thu Feb 13 07:53:10 GMT+07:00 2014]".Illegal path attribute "/wp-content/plugins". Path of origin: "/api/auth/generate_auth_cookie/"
Cookie rejected: "BasicClientCookie[version=0,name=wordpress_654732f696815924ebd07fb96f161421,domain=[my-domain],path=/wp-admin,expiry=Thu Feb 13 07:53:10 GMT+07:00 2014]". Illegal path attribute "/wp-admin". Path of origin: "/api/auth/generate_auth_cookie/"
我搜索了论坛,并尝试了给出的解决方案,但它仍然拒绝来自应用程序的 cookie。我尝试过的几种解决方案是:
-
设置cookie策略
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
CookiePolicy.BROWSER_COMPATIBILITY);
这个例子(here https://stackoverflow.com/questions/4291241/java-htmlunit-cant-login-to-wordpress/4364330#4364330)
This https://stackoverflow.com/questions/874903/how-can-i-force-javas-httpclient-to-accept-invalid-cookies解决了,还是没有结果
这是我到目前为止发送http post数据的android代码
CookieStore cookieStore = new BasicCookieStore();
httpclient.setCookieStore(cookieStore);
CookieSpecFactory csf = new CookieSpecFactory() {
@Override
public CookieSpec newInstance(HttpParams httpParams) {
return new BrowserCompatSpec(){
@Override
public void validate (Cookie cookie, CookieOrigin origin)
throws MalformedCookieException{
Log.d("COOKIE", "force validate cookie path info: " + cookie.getPath() +
", origin: " + origin.getPath());
}
};
}
};
httpclient.getCookieSpecs().register("custom_validate", csf);
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, "custom_validate");
HttpPost httppost = new HttpPost(url);
if(postData != null){
httppost.setEntity(postData);
}
HttpResponse response = httpclient.execute(httppost);
我已经经历了一整天,但没有结果。
有人可以帮忙吗?