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();
private void findCookies(String cookieHeader[], CookieRecipe recipe) { for (String element : cookieHeader) { recipe.findCookies(element); } }
public PatchedCookieJar(CookieSource source) { try { getAccessibleField(getClass(), "_press").set(this, new PatchedCookiePress(source.getURL())); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } findCookies(source.getHeaderFields("Set-Cookie"), new PatchedRFC2109CookieRecipe()); findCookies(source.getHeaderFields("Set-Cookie2"), new RFC2965CookieRecipe()); }
/** * Creates a cookie jar which is initially populated with cookies parsed from the <code>Set-Cookie</code> and * <code>Set-Cookie2</code> header fields. * <p> * Note that the parsing does not strictly follow the specifications, but * attempts to imitate the behavior of popular browsers. Specifically, * it allows cookie values to contain commas, which the * Netscape standard does not allow for, but which is required by some servers. * </p> */ public CookieJar( CookieSource source ) { _press = new CookiePress( source.getURL() ); findCookies( source.getHeaderFields( "Set-Cookie" ), new RFC2109CookieRecipe() ); findCookies( source.getHeaderFields( "Set-Cookie2" ), new RFC2965CookieRecipe() ); }
public void addTokenWithEqualsSign(CookieRecipe recipe, String token, int equalsIndex) { final String name = token.substring(0, equalsIndex).trim(); final String value = token.substring(equalsIndex + 1).trim(); StringBuffer _value = getFieldValue("_value", StringBuffer.class); _value.insert(0, value); HashMap _attributes = getFieldValue("_attributes", HashMap.class); if (recipe.isCookieAttribute(name.toLowerCase())) { _attributes.put(name.toLowerCase(), _value.toString()); } else { try { getAccessibleMethod(CookiePress.class, "addCookieIfValid", new Class[] { Cookie.class }).invoke(this, new Cookie(name, _value.toString(), _attributes)); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } _attributes.clear(); } _value.setLength(0); }
/** * add the given cookie if it is valid * @param cookie */ private void addCookieIfValid( Cookie cookie ) { if (acceptCookie( cookie )) addUniqueCookie( cookie ); }
/** * Returns an object containing the details of the named cookie * @since [ 1488617 ] alternate patch for cookie bug #1371204 */ public Cookie getCookieDetails( String name ) { return _cookieJar.getCookie( name ); }
@Override protected boolean isCookieReservedWord(String token) { return token.equalsIgnoreCase("httpOnly") || super.isCookieReservedWord(token); } }
void setCookieJar() { try { Field _cookies = getAccessibleField(getClass(), "_cookies"); if (_cookies.get(this) == null) { _cookies.set(this, new PatchedCookieJar(this)); } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } }
/** * Creates a cookie jar which is initially populated with cookies parsed from the <code>Set-Cookie</code> and * <code>Set-Cookie2</code> header fields. * <p> * Note that the parsing does not strictly follow the specifications, but * attempts to imitate the behavior of popular browsers. Specifically, * it allows cookie values to contain commas, which the * Netscape standard does not allow for, but which is required by some servers. * </p> */ public CookieJar( CookieSource source ) { _press = new CookiePress( source.getURL() ); findCookies( source.getHeaderFields( "Set-Cookie" ), new RFC2109CookieRecipe() ); findCookies( source.getHeaderFields( "Set-Cookie2" ), new RFC2965CookieRecipe() ); }
public PatchedCookieJar(CookieSource source) { try { getAccessibleField(getClass(), "_press").set(this, new PatchedCookiePress(source.getURL())); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } findCookies(source.getHeaderFields("Set-Cookie"), new PatchedRFC2109CookieRecipe()); findCookies(source.getHeaderFields("Set-Cookie2"), new RFC2965CookieRecipe()); }
public void addTokenWithEqualsSign(CookieRecipe recipe, String token, int equalsIndex) { final String name = token.substring(0, equalsIndex).trim(); final String value = token.substring(equalsIndex + 1).trim(); StringBuffer _value = getFieldValue("_value", StringBuffer.class); _value.insert(0, value); HashMap _attributes = getFieldValue("_attributes", HashMap.class); if (recipe.isCookieAttribute(name.toLowerCase())) { _attributes.put(name.toLowerCase(), _value.toString()); } else { try { getAccessibleMethod(CookiePress.class, "addCookieIfValid", new Class[] { Cookie.class }).invoke(this, new Cookie(name, _value.toString(), _attributes)); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } _attributes.clear(); } _value.setLength(0); }
/** * add the given cookie if it is valid * @param cookie */ private void addCookieIfValid( Cookie cookie ) { if (acceptCookie( cookie )) addUniqueCookie( cookie ); }
private void findCookies(String cookieHeader[], CookieRecipe recipe) { for (String element : cookieHeader) { recipe.findCookies(element); } }
@Override protected boolean isCookieReservedWord(String token) { return token.equalsIgnoreCase("httpOnly") || super.isCookieReservedWord(token); } }
void setCookieJar() { try { Field _cookies = getAccessibleField(getClass(), "_cookies"); if (_cookies.get(this) == null) { _cookies.set(this, new PatchedCookieJar(this)); } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } }
/** * add the given cookie if it is valid * @param cookie */ private void addCookieIfValid( Cookie cookie ) { if (acceptCookie( cookie )) addUniqueCookie( cookie ); }