【问题标题】:Persisting cookies using httpPost and httpclient使用 httpPost 和 httpclient 持久化 cookie
【发布时间】:2012-08-14 15:28:14
【问题描述】:

我有向网络服务发送请求的类

public class JSONParser {
CookieStore store = new BasicCookieStore();
   static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    private static final DefaultHttpClient httpClient = new DefaultHttpClient() ;
 public static DefaultHttpClient getInstance() { return httpClient; }

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {
        CookieStore cookieStore = new BasicCookieStore();

        // Create local HTTP context
        HttpContext localContext = new BasicHttpContext();

        // Bind custom cookie store to the local context
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        DefaultHttpClient httpClient = JSONParser.getInstance();
        List<Cookie> cookies = httpClient.getCookieStore().getCookies();
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Android-AEApp,ID=2435743");
        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        httpPost.setHeader("User-Agent","Android-AEApp,ID=2435743");

        cookies = httpClient.getCookieStore().getCookies();
        httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpPost.setHeader("Cookie", "PHPSESSID=lc89a2uu0rj6t2p219gc2cq4i2; path=/");
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        cookies = httpClient.getCookieStore().getCookies();

        if (cookies.isEmpty()) {
            for (Cookie a : cookies)
            cookieStore.addCookie(a);
        } else {
            for (Cookie a : cookies) {
                cookieStore.addCookie(a);
                System.out.println("- " + a.getValue().toString());
            }
        }

        is = httpEntity.getContent();


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
   }

我第一次通过

发送 cookie
httpPost.setHeader("Cookie", "PHPSESSID=lc89a2uu0rj6t2p219gc2cq4i2; path=/");

然后我会为新请求获取 cookie。当我尝试更新它们时,它会使用旧的。如果服务器更改了我的 cookie,我该如何更新它们?

【问题讨论】:

    标签: android cookies http-post httpclient cookiestore


    【解决方案1】:

    问题解决了。我将 Singletone 用于我的 Cookie 的 ArrayList。它看起来像这样:

    public class CookieStorage {
    
    
        private ArrayList<Object> arrayList;
    
        private static CookieStorage instance;
    
        private CookieStorage(){
            arrayList = new ArrayList<Object>();
        }
    
        public static CookieStorage getInstance(){
            if (instance == null){
                instance = new CookieStorage();
            }
            return instance;
        }
    
        public ArrayList<Object> getArrayList() {
            return arrayList;
        }
    
        @Override
        public String toString()
        {       
        return getArrayList().toString();   
        }
    }
    

    这就是我在 RestClient 类中使用它的方式:

    if (CookieStorage.getInstance().getArrayList().isEmpty())
            CookieStorage.getInstance().getArrayList().add("PHPSESSID=lc89a2uu0rj6t2p219gc2cq4i2");
        RestClient.this.AddHeader("Cookie",  CookieStorage.getInstance().getArrayList().get(0).toString()); 
    

    请求执行后:

     httpResponse = client.execute(request);
                if (httpResponse.getLastHeader("Set-Cookie")!=null)
                {
                    CookieStorage.getInstance().getArrayList().remove(0);
                    CookieStorage.getInstance().getArrayList().add(httpResponse.getLastHeader("Set-Cookie").getValue());
                }
    

    【讨论】:

    • 你这里真的是指JSONParser吗:DefaultHttpClient httpClient = JSONParser.getInstance();
    • 您从哪里获得 PHPSSID?在:httpPost.setHeader("Cookie", "PHPSESSID=lc89a2uu0rj6t2p219gc2cq4i2; path=/");
    猜你喜欢
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 2011-02-20
    相关资源
    最近更新 更多