How to implement cookie handling on Android using OkHttp?

后端 未结 3 1724
一生所求
一生所求 2020-11-27 12:06

Using OkHttp by Square https://github.com/square/okhttp, how can I:

  1. Retrieve a cookie returned from the server
  2. Store the cookie for upcoming requests<
相关标签:
3条回答
  • 2020-11-27 12:47

    Pre OkHttp 3, you can pass a CookieHandler to your OkHttpClient instance. You can use the CookieManager implementation from java.net or you could implement your own if you want. Choose the policy that works best for your needs.

    OkHttpClient client = new OkHttpClient();
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    client.setCookieHandler(cookieManager);
    

    OkHttp will save cookies received from Responses into the CookieHandler and read from it when sending requests. It will do so for matching request/response URIs.

    0 讨论(0)
  • 2020-11-27 13:05

    I needed to share the default Cookie Jar (CookieManager.getInstance()) so this seemed to work ok for me.

    return new CookieJar() {
    
            @Override
            public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                CookieManager cookieManager = CookieManager.getInstance();
    
                for (Cookie cookie : cookies) {
                    cookieManager.setCookie(url.toString(), cookie.toString());
                }
            }
    
            @Override
            public List<Cookie> loadForRequest(HttpUrl url) {
                CookieManager cookieManager = CookieManager.getInstance();
                List<Cookie> cookies = new ArrayList<>();
                if (cookieManager.getCookie(url.toString()) != null) {
                    String[] splitCookies = cookieManager.getCookie(url.toString()).split("[,;]");
                    for (int i=0; i<splitCookies.length; i++) {
                        cookies.add(Cookie.parse(url, splitCookies[i].trim()));
                    }
                }
                return cookies;
            }
        };
    
    0 讨论(0)
  • 2020-11-27 13:08

    For OkHttp3, a simple accept-all, non-persistent CookieJar implementation can be as follows:

    OkHttpClient client = new OkHttpClient.Builder()
        .cookieJar(new CookieJar() {
            private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();
    
            @Override
            public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                cookieStore.put(url, cookies);
            }
    
            @Override
            public List<Cookie> loadForRequest(HttpUrl url) {
                List<Cookie> cookies = cookieStore.get(url);
                return cookies != null ? cookies : new ArrayList<Cookie>();
            }
        })
        .build();
    

    Or if you prefer to use java.net.CookieManager, include okhttp-urlconnection in your project, which contains JavaNetCookieJar, a wrapper class that delegates to java.net.CookieHandler:

    dependencies {
        compile "com.squareup.okhttp3:okhttp:3.0.0"
        compile "com.squareup.okhttp3:okhttp-urlconnection:3.0.0"
    }
    

    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    OkHttpClient client = new OkHttpClient.Builder()
        .cookieJar(new JavaNetCookieJar(cookieManager))
        .build();
    
    0 讨论(0)
提交回复
热议问题