Skip to content

Instantly share code, notes, and snippets.

@khris
Last active December 11, 2015 21:39
Show Gist options
  • Save khris/4664150 to your computer and use it in GitHub Desktop.
Save khris/4664150 to your computer and use it in GitHub Desktop.
Cookie synchronization between WebView and HttpClient
CookieManager cookieManager = CookieManager.getInstance();
BasicCookieStore cookieStore = new BasicCookieStore();
final String appDomain = mContext.getResources().getString(R.string.app_domain);
String cookies = cookieManager.getCookie(appDomain);
String[] splitCookies = cookies.split(";");
for (String cookie : splitCookies) {
String[] cookieParts = cookie.split("=");
if (cookieParts.length > 0) {
String cookieValue = "";
if (cookieParts.length >= 2) {
cookieValue = cookieParts[1];
}
BasicClientCookie newCookie = new BasicClientCookie(cookieParts[0].trim(), cookieValue);
cookieStore.addCookie(newCookie);
}
}
cookieManager.removeExpiredCookie();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment