Android httpclient cookie拒绝了非法路径属性

时间:2014-01-29 13:37:22

标签: java android wordpress cookies

我正在构建一个使用httpclient发布和检索wordpress服务器数据的android应用程序。

由于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

  • This解决方案,仍然没有结果

这是我的Android代码到目前为止发送http帖子数据

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

我整天都在经历这一切,但没有结果。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

尝试以下步骤:

  1. 您必须在Web服务调用公共类

    上声明cookie列表静态变量
    public static List<Cookie> cookies;
    
  2. 当您登录时,从respone httpclient获取cookie并将其分配给静态cookie列表。 //这里我给了演示登录请求

    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();
        }
    }
    
  3. 现在根据登录cookie获取httpclient对象,以获取其余的服务器请求。

    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;
    }
    
相关问题