简体   繁体   中英

OkHttp3 not sending cookies

Here's my OkHttp3 client, cookie and interceptor declarations:

CookieJar cookieJar = new CookieJar() {
        private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();

        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
            //System.out.println("added new cookies: "+cookies);
            cookieStore.put(url.host(), cookies);
            //System.out.println("list of all cookies: "+cookieStore);
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url) {
            List<Cookie> cookies = cookieStore.get(url.host());
            System.out.println();
            return cookies != null ? cookies : new ArrayList<Cookie>();
        }
    };
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS);

    private OkHttpClient sisgradRequest = new OkHttpClient.Builder()
            .cookieJar(cookieJar)
            .addNetworkInterceptor(new UserAgentInterceptor(userAgent))
            .addInterceptor(logging)
            .build();

It's inside a class. Lots of methods execute method calls from this OkHttpClient called sisgradRequest, for example:

Request fakeUserNavigation = new Request.Builder().
                url(protocol + "://" + domain + "/" + "sentinela" + "/").
                build();
        Response a  = sisgradRequest.newCall(fakeUserNavigation).execute();

In the logging information, I can see this:

Aug 16, 2016 8:20:22 PM okhttp3.internal.platform.Platform log

INFO: Set-Cookie: JSESSIONID=E793...31C2.sis_sentinela_worker_6; Path=/sentinela/; Secure; HttpOnly

So, cookies are being delivered by this first call. Then, I make another call, like this:

Request fakeUserNavigation2 = new Request.Builder().
                url(protocol + "://" + domain + "/" + "sentinela" + "/" + "login.open.action").
                build();

        Response b = sisgradRequest.newCall(fakeUserNavigation2).execute();

but the GET request is logged like this:

INFO: --> GET https://.../sentinela/login.open.action http/1.1
Aug 16, 2016 8:20:22 PM okhttp3.internal.platform.Platform log
INFO: --> END GET

This is the entire request, because it has END GET. No cookies are being sent. All the method calls are using the same OkHttp3Client, so the cookies should be shared among them. I tried all the 3 implementations of cookie jars from here , but none of them work. This one in my code is from the last answer.

It looks like the response you get from the server is not trying to update the cookies and you might be replacing the existing cookies with an empty List. But slight tweaks to your debugging printlns should confirm that.

Why not only replace the cookies in saveFromResponse if the list is non empty?

@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
    System.out.println("received cookies from server: " + cookies);

    if (cookies.size() > 0) {
        cookieStore.put(url.host(), cookies);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM