Storing and restoring cookies in Android (persistent cookie storage)

后端 未结 3 790
忘掉有多难
忘掉有多难 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:13

    Storing some data in CookieManager:

    void populateCookieStore(URI uri)
            throws IOException {
    
        CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
    
        CookieHandler.setDefault(cm);
    
        Map> header = new HashMap<>();
    
        List 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);
    }
    

提交回复
热议问题