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

Jas*_*son 5 java cookies wordpress android

我正在构建一个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/"
Run Code Online (Sandbox Code Playgroud)

我搜索了论坛,并尝试了解决方案,但它仍然拒绝来自应用程序的cookie.我尝试过的几个解决方案是:

这是我的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);
Run Code Online (Sandbox Code Playgroud)

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

Har*_*ana 2

尝试以下步骤:

  1. 你必须在你的Web服务调用公共类上声明cookies列表静态变量

    public static List<Cookie> cookies;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 当您登录时,从响应 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();
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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;
    }
    
    Run Code Online (Sandbox Code Playgroud)