Android的HttpClient的饼干拒绝非法路径属性 [英] Android httpclient cookie rejected illegal path attribute
问题描述
我要建一个Android应用程序,它使用的HttpClient发布和检索数据,以一个字preSS服务器。
I'm building an android application which use httpclient to post and retrieve data to a wordpress server.
我无法发送,因为cookie中的无效路径后的数据。下面是我检索到的日志:
I can't send a post data because of the invalid path in the cookie. Here's the log I retrieved:
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。一些解决方案,我试过有:
I've searched the forum, and tried solutions given, but it still rejects the cookie from the app. Several solutions I've tried are:
-
设置cookie策略
set cookie policy
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
CookiePolicy.BROWSER_COMPATIBILITY);
这个例子(<一href="http://stackoverflow.com/questions/4291241/java-htmlunit-cant-login-to-word$p$pss/4364330#4364330">here)
<一个href="http://stackoverflow.com/questions/874903/how-can-i-force-javas-httpclient-to-accept-invalid-cookies">This解决办法,还是没有结果。
This solution, still no result
这是我的android code到目前为止发送HTTP POST数据。
here's my android code so far for sending http post data
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);
我已经经历了这一切的一天走了,但没有结果。 任何人都可以帮忙吗?
I've gone through this all day, but no results. Can anyone help?
推荐答案
请尝试以下步骤:
-
您必须对您的Web服务调用常用的类中声明的Cookie列表静态变量
you have to declare cookies list static variable on you web service call common class
public static List<Cookie> cookies;
当您登录从respone的HttpClient获取饼干并将其分配给静态的Cookie列表。 //下面笔者就给演示登录请求
when you login get cookies from respone httpclient and assign it to static cookies list. // here i gave demo login request
private void login(){
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
if(postData != null){
httppost.setEntity(postData);
}
HttpResponse response = httpclient.execute(httppost);
try {
cookies = httpclient.getCookieStore().getCookies();
} catch (Exception e) {
}
} catch (Throwable e) {
e.printStackTrace();
}
}
现在获得登录的Cookie HttpClient的对象基地的服务器请求的其余部分。
now get httpclient object base on login cookies for rest of server request.
public DefaultHttpClient getHttpclient() {
DefaultHttpClient httpclient = new DefaultHttpClient();
if (cookies != null) {
int size = cookies.size();
for (int i = 0; i < size; i++) {
httpclient.getCookieStore().addCookie(cookies.get(i));
}
}
// you have also set your extra properties of httpclient like user-agent and time etc. here
return httpclient;
}
这篇关于Android的HttpClient的饼干拒绝非法路径属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!