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.