Skip to content

Instantly share code, notes, and snippets.

@Jthomas54
Last active May 29, 2019 04:59
Show Gist options
  • Save Jthomas54/2f34b8aea5b457db5459e2421deffd15 to your computer and use it in GitHub Desktop.
Save Jthomas54/2f34b8aea5b457db5459e2421deffd15 to your computer and use it in GitHub Desktop.
Cookie jar that handles syncing okhttp cookies with Webview cookie manager
import android.webkit.CookieManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
/**
* Provides a synchronization point between the webview cookie store and okhttp3.OkHttpClient cookie store
*/
public final class WebviewCookieHandler implements CookieJar {
private CookieManager webviewCookieManager = CookieManager.getInstance();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
String urlString = url.toString();
for (Cookie cookie : cookies) {
webviewCookieManager.setCookie(urlString, cookie.toString());
}
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
String urlString = url.toString();
String cookiesString = webviewCookieManager.getCookie(urlString);
if (cookiesString != null && !cookiesString.isEmpty()) {
//We can split on the ';' char as the cookie manager only returns cookies
//that match the url and haven't expired, so the cookie attributes aren't included
String[] cookieHeaders = cookiesString.split(";");
List<Cookie> cookies = new ArrayList<>(cookieHeaders.length);
for (String header : cookieHeaders) {
Cookie c = Cookie.parse(url, header);
if(c != null) {
cookies.add(c)
}
}
return cookies;
}
return Collections.emptyList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment