【问题标题】:Android httpclient cookie rejected illegal path attributeAndroid httpclient cookie 拒绝非法路径属性
【发布时间】:2014-02-21 08:29:32
【问题描述】:

我正在构建一个使用 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解决,还是没有结果

到目前为止,这是我用于发送 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);

我已经经历了一整天,但没有结果。 有人可以帮忙吗?

【问题讨论】:

  • 如何在链接中将数据作为参数发送?
  • 我尝试了上面的代码,它对我有用,我在 asyntask 中调用 httpclient 代码并点击 -blog.com/wp-login.php?带有我的用户名和密码的 url 并在 webview 中显示响应,它显示了我记录的页面。如果没有 CookieSpecFactory impl,它会向我显示 Cookie 拒绝警告并继续显示登录页面。
  • 顺便问一下,你打的是什么网址,什么是postdata?

标签: java android wordpress cookies


【解决方案1】:

试试这些步骤:

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

    public static List<Cookie> cookies;
    
  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();
        }
    }
    
  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;
    }
    

【讨论】:

  • 谢谢。 httpclient 和 cookiestore 应该是静态的,并在每个请求中重用
猜你喜欢
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 2018-09-30
  • 2016-05-26
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 2011-03-29
相关资源
最近更新 更多