Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android httpclient cookie rejected illegal path attribute

I'm building an android application which use httpclient to post and retrieve data to a wordpress server.

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/"

I've searched the forum, and tried solutions given, but it still rejects the cookie from the app. Several solutions I've tried are:

  • set cookie policy

    httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
    CookiePolicy.BROWSER_COMPATIBILITY);
    
  • This example (here)

  • This solution, still no result

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?

like image 856
Jason Avatar asked Nov 01 '22 06:11

Jason


1 Answers

Try these steps:

  1. you have to declare cookies list static variable on you web service call common class

    public static List<Cookie> cookies;
    
  2. 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();
        }
    }
    
  3. 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;
    }
    
like image 78
Haresh Chhelana Avatar answered Nov 09 '22 05:11

Haresh Chhelana