0
这里是我的OkHttp3客户端,饼干和拦截声明:OkHttp3不发送的cookie
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();
这是一个类的内部。方法很多,从这个OkHttpClient称为sisgradRequest执行方法调用,例如:
Request fakeUserNavigation = new Request.Builder().
url(protocol + "://" + domain + "/" + "sentinela" + "/").
build();
Response a = sisgradRequest.newCall(fakeUserNavigation).execute();
在记录的信息,我可以看到这一点:
2016年8月16日下午8时20分22秒okhttp3.internal。 platform.Platform日志
INFO: Set-Cookie: JSESSIONID=E793...31C2.sis_sentinela_worker_6; Path=/sentinela/; Secure; HttpOnly
所以,Cookie正在通过这个第一个电话交付。于是,我拨打另一个电话,就像这样:
Request fakeUserNavigation2 = new Request.Builder().
url(protocol + "://" + domain + "/" + "sentinela" + "/" + "login.open.action").
build();
Response b = sisgradRequest.newCall(fakeUserNavigation2).execute();
但GET请求记录是这样的:
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
这是整个请求,因为它到底得的。没有Cookie正在发送。所有的方法调用都使用相同的OkHttp3Client,所以Cookie应该在它们之间共享。我尝试了所有3个来自here的饼干罐的实现,但都没有工作。我的代码中的这一个来自最后一个答案。
我有同样的问题。你有没有设法解决这个问题? – Tooroop