2014-02-17 43 views
0

我正在編寫一個登錄系統,它將保持用戶永久登錄(直到用戶名或密碼不正確),但我在使用cookie存儲時遇到問題。我想要做的是將cookies存儲在本地存儲中(可能是共享首選項)。雖然我不知道從哪裏開始。這是我的主要HTTP Post功能。存儲和使用cookies與HTTPPost和HTTPClient的最佳方式?

HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(url); 
    try { 
     // Add your data 
     httppost.setHeader("X-Requested-With", "XMLHttpRequest"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 

     StringBuffer sb = new StringBuffer(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent())); 
     String line; 
     while ((line = rd.readLine()) != null) { 
      sb.append(line); 
     } 
     rd.close(); 
     return sb.toString(); 
    } catch (Exception e) { 
     //TODO: WIP 
     e.printStackTrace(); 
    } 

我想先設置cookie(當然如果有的話),那麼我想在httppost執行後重新保存它們。我可以在哪裏做這件事?

編輯有大約4個保存的cookie。

回答

相關問題