Skip to content

Instantly share code, notes, and snippets.

@jodaka
Created May 12, 2014 15:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jodaka/f4d5998e6faaf97075ca to your computer and use it in GitHub Desktop.
Save jodaka/f4d5998e6faaf97075ca to your computer and use it in GitHub Desktop.
Webkit cookies proxy for android-async-http
/**
* We need dummy cookie storage that wouldn't save any cookies
* or get any cookies
*/
public class WebkitCookietHandlerProxy extends CookieHandler {
@Override
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException {
if (uri == null || responseHeaders == null) {
return;
}
String url = uri.toString();
for (String headerKey : responseHeaders.keySet()) {
if (headerKey == null || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie"))) {
continue;
}
for (String headerValue : responseHeaders.get(headerKey)) {
CookieManager.getInstance().setCookie(url, headerValue);
}
CookieSyncManager.getInstance().sync();
}
}
@Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException {
if (uri == null || requestHeaders == null) {
throw new IllegalArgumentException("Argument is null");
}
String url = uri.toString();
Map<String, List<String>> res = new java.util.HashMap<String, List<String>>();
String cookie = CookieManager.getInstance().getCookie(url);
if (cookie != null) {
res.put("Cookie", Arrays.asList(cookie));
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment