Storing and restoring cookies in Android (persistent cookie storage)

后端 未结 3 784
忘掉有多难
忘掉有多难 2021-01-16 16:40

Searched a lot.

I have an app. App logins on server and receive some cookies, then it can execute some POST requests with them (e.g. to get user profile). I want to

相关标签:
3条回答
  • 2021-01-16 17:01

    An implementation of java.net.CookieStore for providing persistent cookies.

    Use this https://gist.github.com/manishk3008/2a2373c6c155a5df6326

    0 讨论(0)
  • 2021-01-16 17:13

    Storing some data in CookieManager:

    void populateCookieStore(URI uri)
            throws IOException {
    
        CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
    
        CookieHandler.setDefault(cm);
    
        Map<String,List<String>> header = new HashMap<>();
    
        List<String> values = new ArrayList<>();
    
        values.add("JSESSIONID=" + SESSION_ID + "; version=1; Path="
                   + URI_PATH +"; HttpOnly");
    
        values.add("CUSTOMER=WILE_E_COYOTE; version=1; Path=" + URI_PATH);
    
        header.put("Set-Cookie", values);
    
        cm.put(uri, header);
    }
    
    0 讨论(0)
  • 2021-01-16 17:22

    Now I'm using a PersistentCookieStorage class for those purposes. I create a singleton when the app is launched. It stores cookies in SharedPreferences.

    0 讨论(0)
提交回复
热议问题