Skip to content

Instantly share code, notes, and snippets.

@gtomek
Created April 30, 2020 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtomek/85db01fdfc8a8f7860339d4aabc8dcd5 to your computer and use it in GitHub Desktop.
Save gtomek/85db01fdfc8a8f7860339d4aabc8dcd5 to your computer and use it in GitHub Desktop.
Add cookie jar to OkHttp client
class OkHttpCookieJar {
/**
Requires okhttp-urlconnection aftefact
**/
public OkHttpClient getOkHttpWithCookies() {
// set cookies
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
return new OkHttpClient.Builder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment