简体   繁体   English

OkHttp3不发送cookie

[英]OkHttp3 not sending cookies

Here's my OkHttp3 client, cookie and interceptor declarations: 这是我的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();

It's inside a class. 它在一个类中。 Lots of methods execute method calls from this OkHttpClient called sisgradRequest, for example: 许多方法从此OkHttpClient执行名为sisgradRequest的方法调用,例如:

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 2016年8月16日8:20:22 PM okhttp3.internal.platform.Platform日志

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

So, cookies are being delivered by this first call. 因此,Cookie是通过第一个调用传递的。 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: 但是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

This is the entire request, because it has END GET. 这是整个请求,因为它具有END GET。 No cookies are being sent. 没有发送Cookie。 All the method calls are using the same OkHttp3Client, so the cookies should be shared among them. 所有方法调用都使用相同的OkHttp3Client,因此应在它们之间共享cookie。 I tried all the 3 implementations of cookie jars from here , but none of them work. 我从这里尝试了cookie罐的所有3种实现,但是它们都不起作用。 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. 您从服务器获得的响应似乎没有尝试更新cookie,并且您可能将现有的cookie替换为空的List。 But slight tweaks to your debugging printlns should confirm that. 但是对调试printlns的细微调整应该可以确认这一点。

Why not only replace the cookies in saveFromResponse if the list is non empty? 如果列表不为空,为什么不只替换saveFromResponse中的cookie?

@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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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