TAGS :Viewed: 9 - Published at: a few seconds ago

[ Multiple Cookies Headers OKHttp JavaNetCookieJar ]

I've been using to JavaNetCookieJar OKHttp Library and the code is in the following link.

https://github.com/square/okhttp/blob/master/okhttp-urlconnection/src/main/java/okhttp3/JavaNetCookieJar.java

JavaNetCookieJar seem to have issues where it only adds the very first cookie to the request tough the previous response has received multiple "Set-Cookie"

After digging down the git-hub code looks like I've to specify the list of cookies explicitly based on the following code "// The RI passes all headers. We don't have 'em, so we don't pass 'em!"

 @Override public List<Cookie> loadForRequest(HttpUrl url) {
        // The RI passes all headers. We don't have 'em, so we don't pass 'em!
        Map<String, List<String>> headers = Collections.emptyMap();
        Map<String, List<String>> cookieHeaders;
        try {
            cookieHeaders = cookieHandler.get(url.uri(), headers);
        } catch (IOException e) {
            Platform.get().log(WARN, "Loading cookies failed for " + url.resolve("/..."), e);
            return Collections.emptyList();
        }

        List<Cookie> cookies = null;
        for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) {
            String key = entry.getKey();
            if (("Cookie".equalsIgnoreCase(key) || "Cookie2".equalsIgnoreCase(key))
                    && !entry.getValue().isEmpty()) {
                for (String header : entry.getValue()) {
                    if (cookies == null) cookies = new ArrayList<>();
                    cookies.addAll(decodeHeaderAsJavaNetCookies(url, header));
                }
            }
        }

        return cookies != null
                ? Collections.unmodifiableList(cookies)
                : Collections.<Cookie>emptyList();
    } 

Can someone please help me how to insatiate this without hardcoding the list of cookies I'm interested as I need all the cookies.

Any help appreciated . Thanks

Answer 1


I ended-up doing this but any robust solution is appreciated

public class CookieStore implements CookieJar {

    private final HashMap<String, List<Cookie>> _cookieStore = new HashMap<>();

    @Override
    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {

        List<Cookie> currentCookies = _cookieStore.get(url.host());
        List<Cookie> responseCookies = new ArrayList(cookies);

        if (currentCookies != null && responseCookies != null
                && responseCookies.size() > 0 && currentCookies.size() > 0) {
            for (Cookie currentCookie : currentCookies) {

                if (!responseCookies.contains(currentCookie) && (currentCookie.value()!=null && currentCookie.value()!="")) {
                    responseCookies.add(currentCookie);
                }
            }
        }

        _cookieStore.put(url.host(), responseCookies);
    }

    @Override
    public List<Cookie> loadForRequest(HttpUrl url) {

        List<Cookie> cookies = _cookieStore.get(url.host());
        List<Cookie> requestCookies = new ArrayList<>();

        if (cookies != null && cookies.size() > 0) {
            for (Cookie currentCookie : cookies) {
                if ((currentCookie.value()!=null && currentCookie.value()!="")) {
                    requestCookies.add(currentCookie);
                }
            }
        }

        return requestCookies != null ? requestCookies : new ArrayList<Cookie>();
    }