Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am using okhttp3 followed by retrofit2 and the next time cookies are lost #14

Closed
TrAnmol opened this issue Aug 3, 2016 · 9 comments
Closed
Labels

Comments

@TrAnmol
Copy link

TrAnmol commented Aug 3, 2016

I am using okhttp3 followed by retrofit2 and the next time cookies are lost below is my code and

public static ClearableCookieJar cookieManager = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context)); public static ClearableCookieJar getCookieManager() { return cookieManager; }

`OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(NETWORK_TIMEOUT, TimeUnit.SECONDS);
builder.readTimeout(READ_TIMEOUT, TimeUnit.MINUTES);
builder.cookieJar(TrestApplication.getCookieManager());

    OkHttpClient clientOkHttp = builder.build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(clientOkHttp)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    return retrofit;`
@sunnykinger
Copy link

I am having same problem. When I restart the application then cookies are lost.

@franmontiel
Copy link
Owner

Have you checked that your cookies have an expire or max-age attribute? If they do not they are session cookies and should be "lost" each time you reset your app.

@sunnykinger
Copy link

Yes it was a session thing you mentioned and I made it to work using a remember parameter in the API. I am facing another issue. There is a comment endpoint which posts new comment on server, when I hit the endpoint on first attempt, it doesn't works but on the second attempt it works and responds accordingly. On the first attempt it is not able to use the cookies thing as it asks me to login first.

@Leonid-Kiev
Copy link

Leonid-Kiev commented Jan 25, 2017

Yeah, seems the library is useless as cookies are being cleared after application restart.
There is no point in this kind of "persistence"

@franmontiel
Copy link
Owner

@Leonid-Kiev if you have any problem you are welcome to create a test reproducing the issue. Even you can create a PR fixing the issue. Although my first recommendation would be to check if your cookies attributes are the correct ones according to the RFC 6265

@Leonid-Kiev
Copy link

Thanks.
I ended up using known PersistentCookieStore, which works fine as for now and stores cookies in persistent with OkHttp3 + Retrofit 2 so they are still available after app restart.
Anyway thanks for your lib

@wangdandong
Copy link

In class PersistentCookieJar method saveFromResponse
change persistor.saveAll(filterPersistentCookies(cookies)); to persistor.saveAll(cookies);

@franmontiel
Copy link
Owner

@wangdandong with your solution the library will persist all cookies, if you do not have access to the server to configure the cookies properly can be good workaround but the real good solution is to configure correctly the cookies with an expire date so the cookies can be persisted.

A more elegant workaround when you do not have access to the server but you need to persist the cookies is to create a OkHttp Interceptor that takes the incoming cookies and add to them a expire date.

@KomoriWu
Copy link

KomoriWu commented Apr 2, 2018

@wangdandong 非常感谢,之前的问题就是框架没保存sessionID 。很奇怪为什么这个框架默认要过滤掉sessionID。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants