2014-01-29 46 views
5

我正在構建一個android應用程序,它使用httpclient將數據發佈並檢索到wordpress服務器。Android httpclient cookie拒絕非法路徑屬性

由於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 post d ata

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

我已經經歷了這一整天,但沒有結果。 任何人都可以幫忙嗎?

+0

有關發送數據作爲鏈接參數是什麼? – Rokas

+0

我試過上面的代碼對我有用,我打電話給asyntask中的httpclient代碼並點擊-http://blog.com/wp-login.php?與我的用戶名和pwd的網址,並顯示webview中的響應它顯示我登錄的頁面。如果沒有CookieSpecFactory,則會顯示Cookie曲目警告並繼續顯示登錄頁面。 – Neha

+0

順便說一句,什麼網址打什麼postdata? – Neha

回答

2

請嘗試以下步驟:

  1. ,你必須在你的Web服務調用通用類

    public static List<Cookie> cookies; 
    
  2. 當您登錄時從輸入反應的HttpClient獲取餅乾並將其分配給申報Cookie清單靜態變量靜態cookies列表。 //下面筆者就給演示登錄請求

    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. 現在得到的HttpClient對象基礎上,登錄cookie進行服務器請求的其餘部分。

    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; 
    } 
    
+0

謝謝。 httpclient和cookiestore應該是靜態的,並且可以在每個請求中重複使用 – Jason

+0

@ Jason,嗨,如果對您有用,謝謝... –