How do I persist cookies when using HTTPUrlConnection?

后端 未结 4 878
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 03:33

I\'ve begun using the recommended HTTPUrlConnection and moved away from the DefaultHTTPClient. One of the things that I haven\'t been able to glue

4条回答
  •  醉话见心
    2020-12-01 04:31

    Its' taken me a few hours but I managed to build a custom cookie storage myself.

    You have to attach this by doing this:

    public class application extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
           CookieManager cmrCookieMan = new CookieManager(new MyCookieStore(this.objContext), CookiePolicy.ACCEPT_ALL);
           CookieHandler.setDefault(cmrCookieMan);
           }
        }
    

    Here's the actual storage:

    /*
     * This is a custom cookie storage for the application. This
     * will store all the cookies to the shared preferences so that it persists
     * across application restarts.
     */
    class MyCookieStore implements CookieStore {
    
        /*
         * The memory storage of the cookies
         */
        private Map> mapCookies = new HashMap>();
        /*
         * The instance of the shared preferences
         */
        private final SharedPreferences spePreferences;
    
        /*
         * @see java.net.CookieStore#add(java.net.URI, java.net.HttpCookie)
         */
        public void add(URI uri, HttpCookie cookie) {
    
            System.out.println("add");
            System.out.println(cookie.toString());
    
            List cookies = mapCookies.get(uri);
            if (cookies == null) {
                cookies = new ArrayList();
                mapCookies.put(uri, cookies);
            }
            cookies.add(cookie);
    
            Editor ediWriter = spePreferences.edit();
            HashSet setCookies = new HashSet();
            setCookies.add(cookie.toString());
            ediWriter.putStringSet(uri.toString(), spePreferences.getStringSet(uri.toString(), setCookies));
            ediWriter.commit();
    
        }
    
       /*
        * Constructor
        * 
        * @param  ctxContext the context of the Activity
        */
        @SuppressWarnings("unchecked")
        public MyCookieStore(Context ctxContext) {
    
            spePreferences = ctxContext.getSharedPreferences("CookiePrefsFile", 0);
            Map prefsMap = spePreferences.getAll();
    
            for(Map.Entry entry : prefsMap.entrySet()) {
    
                for (String strCookie : (HashSet) entry.getValue()) {
    
                    if (!mapCookies.containsKey(entry.getKey())) {
    
                        List lstCookies = new ArrayList();
                        lstCookies.addAll(HttpCookie.parse(strCookie));
    
                        try {
    
                            mapCookies.put(new URI(entry.getKey()), lstCookies);
    
                        } catch (URISyntaxException e) {
    
                            e.printStackTrace();
    
                        }
    
                    } else {
    
                        List lstCookies = mapCookies.get(entry.getKey());
                        lstCookies.addAll(HttpCookie.parse(strCookie));
    
                        try {
    
                            mapCookies.put(new URI(entry.getKey()), lstCookies);
    
                        } catch (URISyntaxException e) {
    
                            e.printStackTrace();
    
                        }
    
                    }
    
                    System.out.println(entry.getKey() + ": " + strCookie);
    
                }
    
            }
    
        }
    
        /*
         * @see java.net.CookieStore#get(java.net.URI)
         */
        public List get(URI uri) {
    
            List lstCookies = mapCookies.get(uri);
    
            if (lstCookies == null)
                mapCookies.put(uri, new ArrayList());
    
            return mapCookies.get(uri);
    
        }
    
        /*
         * @see java.net.CookieStore#removeAll()
         */
        public boolean removeAll() {
    
            mapCookies.clear();
            return true;
    
        }        
    
        /*
         * @see java.net.CookieStore#getCookies()
         */
        public List getCookies() {
    
            Collection> values = mapCookies.values();
    
            List result = new ArrayList();
            for (List value : values) {                
                result.addAll(value);                
            }
    
            return result;
    
        }
    
        /*
         * @see java.net.CookieStore#getURIs()
         */
        public List getURIs() {
    
            Set keys = mapCookies.keySet();
            return new ArrayList(keys);
    
        }
    
        /*
         * @see java.net.CookieStore#remove(java.net.URI, java.net.HttpCookie)
         */
        public boolean remove(URI uri, HttpCookie cookie) {
    
            List lstCookies = mapCookies.get(uri);
    
            if (lstCookies == null)
                return false;
    
            return lstCookies.remove(cookie);
    
        }
    
    }
    

提交回复
热议问题