franmontiel / persistentcookiejar Goto Github PK
View Code? Open in Web Editor NEWA persistent CookieJar implementation for OkHttp 3 based on SharedPreferences.
License: Apache License 2.0
A persistent CookieJar implementation for OkHttp 3 based on SharedPreferences.
License: Apache License 2.0
I was using the Cookie Persistor, I noticed that the removeAll method is being called if there are no cookies on the REST return. Is that correct? This prevents me from using the cookies previously stored.
Thanks for the beautiful implementation. But regarding the included proguard rules, they basically keep everything of your library, which I believe is redundant. Can you clarify more on your thought about that setup? Thanks.
签名打包安装进入应用,从后台杀掉应用。从新进入就报NullPointerException error。cookie反序列化的时候失败,导致cookie为空。我打印了日志貌似从sp中获取到的字符串,就不是正常的cookie序列化后的字符串。烦请有知道原因的帮忙解决一下。多谢
06-26 10:08:00.289 18000-18000/? E/HuiMai.go: crash result ==java.lang.ExceptionInInitializerError
at cn.hhealth.shop.net.k.(Novate.java:104)
at cn.hhealth.shop.net.c.(BaseRequest.java:24)
at cn.hhealth.shop.d.l.(CommonRequest.java:27)
at cn.hhealth.shop.service.SettingService.onCreate(SettingService.java:54)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2990)
at android.app.ActivityThread.access$1900(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5542)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:962)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.Cookie.name()' on a null object reference
at cn.hhealth.shop.net.a.e.hashCode(IdentifiableCookie.java:57)
at java.util.Collections.secondaryHash(Collections.java:3405)
at java.util.HashMap.remove(HashMap.java:616)
at java.util.HashSet.remove(HashSet.java:173)
at java.util.Collections$SynchronizedCollection.remove(Collections.java:422)
at cn.hhealth.shop.net.a.c.a(CookieCacheImpl.java:36)
at cn.hhealth.shop.net.a.f.(NovateCookieManager.java:28)
at cn.hhealth.shop.net.s.a(RetrofitBuilder.java:85)
at cn.hhealth.shop.net.s.(RetrofitBuilder.java:51)
at cn.hhealth.shop.net.s.(RetrofitBuilder.java:39)
at cn.hhealth.shop.net.k.(Novate.java:104)
at cn.hhealth.shop.net.c.(BaseRequest.java:24)
at cn.hhealth.shop.d.l.(CommonRequest.java:27)
at cn.hhealth.shop.service.SettingService.onCreate(SettingService.java:54)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2990)
at android.app.ActivityThread.access$1900(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5542)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:962)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.Cookie.name()' on a null object reference
at cn.hhealth.shop.net.a.e.hashCode(IdentifiableCookie.java:57)
at java.util.Collections.secondaryHash(Collections.java:3405)
at java.util.HashMap.remove(HashMap.java:616)
at java.util.HashSet.remove(HashSet.java:173)
at java.util.Collections$SynchronizedCollection.remove(Collections.java:422)
at cn.hhealth.shop.net.a.c.a(CookieCacheImpl.java:36)
at cn.hhealth.shop.net.a.f.(NovateCookieManager.java:28)
at cn.hhealth.shop.net.s.a(RetrofitBuilder.java:85)
at cn.hhealth.shop.net.s.(RetrofitBuilder.java:51)
at cn.hhealth.shop.net.s.(RetrofitBuilder.java:39)
at cn.hhealth.shop.net.k.(Novate.java:104)
at cn.hhealth.shop.net.c.(BaseRequest.java:24)
at cn.hhealth.shop.d.l.(CommonRequest.java:27)
at cn.hhealth.shop.service.SettingService.onCreate(SettingService.java:54)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2990)
at android.app.ActivityThread.access$1900(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:
Hi~
PersistentCookieJar is helpful for me!
But once I found my request to get three cookie, save only the results of the two, both the cookie value is not the same, I find IdentifiableCookie:
@Override
public boolean equals(Object other) {
if (!(other instanceof IdentifiableCookie)) return false;
IdentifiableCookie that = (IdentifiableCookie) other;
return that.cookie.name().equals(this.cookie.name())
&& that.cookie.domain().equals(this.cookie.domain())
&& that.cookie.path().equals(this.cookie.path())
&& that.cookie.secure() == this.cookie.secure()
&& that.cookie.hostOnly() == this.cookie.hostOnly();
}
In okhttp3 the cookie inside the class methods:
@Override public boolean equals(Object other) {
if (!(other instanceof Cookie)) return false;
Cookie that = (Cookie) other;
return that.name.equals(name)
&& that.value.equals(value)
&& that.domain.equals(domain)
&& that.path.equals(path)
&& that.expiresAt == expiresAt
&& that.secure == secure
&& that.httpOnly == httpOnly
&& that.persistent == persistent
&& that.hostOnly == hostOnly;
}
I do not know which is better~
Thanks.
Got Failed to resolve: com.github.franmontiel:PersistentCookieJar:v1.0.1
when attempting to sync Gradle.
当我使用中文登录名后 ,cookie 保存后 请求服务器获取不到账户密码
when i use chinese as login name ,
then i login success
but , i could not use this cookie to
automatic logon
note: if the login name is English will be ok !
please help !
I use retrofit2.0 and okhttp3.0, all my request are use the call's method "enqueue()", and I try to response one request delay 5 seconds( just like sleep(5) in PHP), the other requests are waiting the blocked one, and when the blocked request response, the other request execute. locks like the request queue being blocked?
I know that when I Login all information of the session will save to Sharepreference of Persistent cookie. If I logout I will clear all data for the session in SharePreference.
Now I want use multiple account that mean:
How do I use the library to implement that ?
I'm not sure how can I check the cookies that are storage by the library, I mean, to see which ones are being saves, and their data, like expiration time, etc
Another question it's that when you clear cookie session, what cookies are you really cleaning there?
Thanks in advance.
There is a function to load all cookies for a target url:
@Override
synchronized public List<Cookie> loadForRequest(HttpUrl url) { ... }
It would be nice if the cookie jar has a function to remove all cookies for a target url too, e.g.:
@Override
synchronized public void removeForRequest(HttpUrl url) { ... }
What's the correct way to remove cookies from the cache and persistent storage at the moment? The only way I can imagine is to clone the cookie list for a given url and change all the expiresAt
values to -1
.
But that's a pretty shit solution because there is no reason to re-construct the whole list as only one variable needs to be changed in order for the cookie jar to remove those cookies at the next loadForRequest
call (then all of those would be expired).
But making the expiresAt
variable public would be no pretty implementation, so a separate function to remove all cookies for a given url (as suggested above) would be nice!
Hello,
I am using the PersistentCookieJar library in my project and I am noticing some incorrect behavior when there are subsequent requests that modify already existing cookies in some very particular cases, which are:
Example:
First request:
header("Set-Cookie: BAD_cookie_with_secure_flag_next_request_unsecure_nonempty_expire=initial_value; path=/; Secure", false);
Second request:
$expiredDate = new \DateTime('now');
$expiredDate->modify('-1 day');
header("Set-Cookie: BAD_cookie_with_secure_flag_next_request_unsecure_nonempty_expire=nonempty_changed_value; expires={$expiredDate->format(DATE_COOKIE)}; path=/;", false);
In this example, after the second request, the value of BAD_cookie_with_secure_flag_next_request_unsecure_nonempty_expire
remains as initial_value
even though it should have been deleted and not sent at all on the next request, since the cookie is expired.
Example:
First request:
header("Set-Cookie: BAD_cookie_with_secure_flag_next_request_unsecure_empty_expire=initial_value; path=/; Secure", false);
Second request:
$expiredDate = new \DateTime('now');
$expiredDate->modify('-1 day');
header("Set-Cookie: BAD_cookie_with_secure_flag_next_request_unsecure_empty_expire=; expires={$expiredDate->format(DATE_COOKIE)}; path=/;", false);
In this example, after the second request, the value of BAD_cookie_with_secure_flag_next_request_unsecure_empty_expire
remains as initial_value
even though it should have been deleted and not sent at all on the next request, since the cookie is expired.
Example:
First request:
header("Set-Cookie: BAD_cookie_without_secure_flag_next_request_secure_empty_expire=initial_value; path=/;", false);
Second request:
$expiredDate = new \DateTime('now');
$expiredDate->modify('-1 day');
header("Set-Cookie: BAD_cookie_without_secure_flag_next_request_secure_empty_expire=; expires={$expiredDate->format(DATE_COOKIE)}; Secure; path=/;", false);
In this example, after the second request, the value of BAD_cookie_without_secure_flag_next_request_secure_empty_expire
remains as initial_value
even though it should have been deleted and not sent at all on the next request, since the cookie is expired.
In all three of these cases, the tests were accessed via HTTPS. Chrome on Android deleted the cookies in all three cases. Desktop Chrome and Safari on Mac OS also deleted the cookies in all cases.
It looks to me like in these cases there is something going wrong in com.franmontiel.persistentcookiejar.PersistentCookieJar#saveFromResponse because the persistor
seems to contain non-persistent cookies (without an expiration date) when it should not contain them.
I think this might be happening because there might duplicate cookies entries getting created because of the Secure flag getting added into these requests. I see that this is the case in com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor#createCookieKey
. Based on the logic in that function, it seems that there is one cookie created for HTTP and another one for HTTPS. The expected behavior is for only one cookie to be created, not dependent upon the Secure flag or protocol.
For reference, the spec for the Secure attribute is here: https://tools.ietf.org/html/rfc6265#section-4.1.2.5
How does Glide get cookies to display images
Will it be multithread problem?
It's downloading a PersistentCookieJar-v1.0.1.aar
file. Where is the jar
file?
Have you uploaded?
I received this stack trace from a user:
com.franmontiel.persistentcookiejar.cache.IdentifiableCookie.hashCode (IdentifiableCookie.java:65)
java.util.HashMap.remove (HashMap.java:624)
java.util.HashSet.remove (HashSet.java:173)
com.franmontiel.persistentcookiejar.cache.SetCookieCache.addAll (SetCookieCache.java:37)
com.franmontiel.persistentcookiejar.PersistentCookieJar.<init> (PersistentCookieJar.java:38)```
android stuido
Error:Could not find com.github.franmontiel:PersistentCookieJar:v0.9.3.
public void saveAll(Collection<Cookie> cookies) {
SharedPreferences.Editor editor = sharedPreferences.edit();
for (Cookie cookie : cookies) {
if (cookie.persistent()) {
editor.putString(createCookieKey(cookie), new SerializableCookie().encode(cookie));
}
}
editor.apply();
}
public void add(HttpUrl url, Cookie cookie) {
String name = getCookieToken(cookie);
//将cookies缓存到内存中 如果缓存过期 就重置此cookie
if (!cookie.persistent()) {
if (!cookies.containsKey(url.host())) {
cookies.put(url.host(), new ConcurrentHashMap<String, Cookie>());
}
cookies.get(url.host()).put(name, cookie);
} else {
if (cookies.containsKey(url.host())) {
cookies.get(url.host()).remove(name);
}
}
//讲cookies持久化到本地
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
prefsWriter.putString(url.host(), TextUtils.join(",", cookies.get(url.host()).keySet()));
prefsWriter.putString(name, encodeCookie(new SerializableOkHttpCookies(cookie)));
prefsWriter.apply();
}
Response Header:
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=E8B4036464C98E104C35E246CA56FAFC; Path=/
Content-Type: application/json;charset=UTF-8
Content-Length: 537
Date: Thu, 04 Aug 2016 10:45:59 GMT
OkHttp-Sent-Millis: 1470307533457
OkHttp-Received-Millis: 1470307533489
cookie.persistent()
Always returns false
? The link method is alway can`t save to SharedPrefs.
help me?
Expose the serializing/deserializing Cookie
process in SharedPrefsCookiePeristor
to allow custom behavior.
I could make a PR for this if you're interested.
Hi! Whenever I try and use your library i always get the following issue
java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
at com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor.loadAll(SharedPrefsCookiePersistor.java:48)
at com.franmontiel.persistentcookiejar.PersistentCookieJar.<init>(PersistentCookieJar.java:38)
I have no idea what is going on and I was wondering if anyone could help me out.
To provide some context the following is the HTTP class I use to preform requests...
public class HTTP {
// Cookie manager
OkHttpClient client;
ClearableCookieJar cookieJar;
public HTTP(SharedPreferences preferences) {
this.cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(preferences));
// Set up the okhttp client
this.client = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.build();
}
public Response performRequest(Request request) throws Exception {
// Perform the request
Response response = client.newCall(request).execute();
// Save the cookies
return response;
}
}
Hi,
I encountered the following exception:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.Cookie.name()' on a null object reference
at com.franmontiel.persistentcookiejar.cache.IdentifiableCookie.hashCode(IdentifiableCookie.java:65)
at java.util.Collections.secondaryHash(Collections.java:3427)
at java.util.HashMap.remove(HashMap.java:616)
at java.util.HashSet.remove(HashSet.java:173)
at com.franmontiel.persistentcookiejar.cache.SetCookieCache.addAll(SetCookieCache.java:37)
at com.franmontiel.persistentcookiejar.PersistentCookieJar.<init>(PersistentCookieJar.java:38)
Happened after leaving the app on the background for an extended time.
Looking at https://jitpack.io/com/github/franmontiel/PersistentCookieJar/v1.0.0/ I can see there's
PersistentCookieJar-v1.0.0.aar
but there's no
PersistentCookieJar-v1.0.0.jar
Could you upload it as well? That would make your library usable also in non-Android projects.
Hello!
I've added repo and dependency exactly as described to my gradle project, when I build it I get such an error:
Could not resolve all dependencies for configuration ':compile'.
> Could not resolve com.github.franmontiel:PersistentCookieJar:v1.0.1.
Required by:
:libjam:unspecified
> Could not resolve com.github.franmontiel:PersistentCookieJar:v1.0.1.
> Could not get resource 'https://jitpack.io/com/github/franmontiel/PersistentCookieJar/v1.0.1/PersistentCookieJar-v1.0.1.pom'.
> Could not GET 'https://jitpack.io/com/github/franmontiel/PersistentCookieJar/v1.0.1/PersistentCookieJar-v1.0.1.pom'.
> peer not authenticated
That said, I can access the pom from my browser. What can it be?
when I added interceptor to okclient, persistentcookiejar does not working.
do you know any reason or solution?
Please guide me how to read cookies by domain & path...
//I'm using like below
ClearableCookieJar cookieJar = new PersistentCookieJar(
new SetCookieCache(), new SharedPrefsCookiePersistor(context));
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.build();
Thanks.
My "MyOkhttp" library on jcenter which extend okhttp3. I want to dependency PersistentCookieJar with pom, but it only release on jitpack.
Can it release on jcenter? Or I fork it and publish with my jcenter account. (with group id 'com.franmontiel')
Look forward to your soonest reply.
Find a lot of documents, methods, or did not find a solution,Keep reporting the mistake,Cannot change dependencies of configuration ':app:_debugAnnotationProcessor' after it has been resolved.May I help you with your view?
Server change the cookies,but if i use OkhttpClientBuilder.cookieJar(cookieJar);
how can it to change the cookies with Server change by Response‘s setCookies ?
I've faced a exception when using ipv6 ip address.
I'm using okhttp3 3.6.0 with PresistentCookieJar.
When try to connect directly using ivp6 address ex: 2001:b011:400a:156e:211:32ff:fe0f:c06b
The exception occur when SerializableCookie try to read cooke from an inputStream through readObject function
final String domain = (String) in.readObject();
builder.domain(domain);`
It using Cookie.Builder to set domain into cookie.
And inside builder.domain, it checks invalidHostname which can not contains ":"
private static boolean containsInvalidHostnameAsciiCodes(String hostnameAscii) {
for (int i = 0; i < hostnameAscii.length(); i++) {
char c = hostnameAscii.charAt(i);
// The WHATWG Host parsing rules accepts some character codes which are invalid by
// definition for OkHttp's host header checks (and the WHATWG Host syntax definition). Here
// we rule out characters that would cause problems in host headers.
if (c <= '\u001f' || c >= '\u007f') {
return true;
}
// Check for the characters mentioned in the WHATWG Host parsing spec:
// U+0000, U+0009, U+000A, U+000D, U+0020, "#", "%", "/", ":", "?", "@", "[", "\", and "]"
// (excluding the characters covered above).
if (" #%/:?@[\\]".indexOf(c) != -1) {
return true;
}
}
return false;
}
ref:
https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/internal/Util.java#L386
But ipv6 address contains colon anyway.
How can I fix this?
Thanks
I have a static instance of ClearableCookieJar and init it once at the beginning.
Is it right way?
from code below may see SetCookieCache caches cookies in a HashSet, which is not thread-safe and gives rise to ConcurrentModificationException if accessed by multiple threads at the same time. Multi-thread accessing is not rare case in OkHttp.
private Set<IdentifiableCookie> cookies;
public SetCookieCache() {
cookies = new HashSet<>();
}
This is a practical modification to make cookie container thread-safe:
cookies = Collections.newSetFromMap(new ConcurrentHashMap<IdentifiableCookie, Boolean>());
Hi,franmontiel
How to clear cookies?
Hi,
We are using this library with OkHttp3 in 2 separate activities.
In the first activity, we are logging in via a POST, and then starting the second activity.
Once there, we request some information to the same server via a GET call (this is a secured endpoint).
This second call is failing saying the request is not authorized.
This system has been on production for years, so we know it's not the issue.
It looks like either the cookies are not being saved on the initial POST response, or sent out on the GET call. I was under the impression they are saved and sent out by this library, is there something extra that needs to be done?
Here's how we're setting up the client (in both activites):
// Have also tried ClearableCookieJar instead of CookieJar
CookieJar cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(this));
OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.build();
Hi, I am using kotlin to do my android project. Im using this library on my other java projects on android and all of them works fine. But when I use this with kotlin the cookie is not getting saved. I want to know whether this has occured to you or do you know the work around for this issue?
I have manually checked using adb commands to read the shared prefrences. While on my java project the cokie is being set but here it isnt. Please respond fast
this is a aar file.
If you make a jar, it would be fine.
I have implemented as you explain in readme. But Google play is rejected my app. That is bcz of using your dependency.
ClearableCookieJar cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.build();
I have requested to login using basic authentication, it redirects to user details.
After it finished, I have checked cookie.
wfwaf-authcookie-c448b9945c37d9cc4b94b09f3d21632a=27419%7Cadministrator%7Cbfbc704c50089e8b848ca10c14b1b74ad4a309992102e3faf98bdd782d6aeb47; expires=Wed, 09 Nov 2016 14:19:08 GMT; path=/; httponly
wfvt_4021301540=5822879cee322; expires=Wed, 09 Nov 2016 02:49:08 GMT; path=/; httponly
wfvt_4021301540=5822879e5f5cc; expires=Wed, 09 Nov 2016 02:49:10 GMT; path=/; httponly
I found wfvt_4021301540 duplicated.
I think one is for example.com, other for www.example.com.
I have requested to example.com.
I there a way to pass the cookie,which is stored for each request to a webview so that webview will also consume the same session
Hello,
I can't install this plugin for some reason
gradle - project
repositories {
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
}
gradle - module
ext {
androidSupportVersion = '25.3.1'
daggerVersion = '2.10'
butterknifeVersion = '8.5.1'
retrofitVersion = '2.2.0'
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile "com.android.support:appcompat-v7:$androidSupportVersion"
compile "com.android.support:design:$androidSupportVersion"
compile "com.google.dagger:dagger:$daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
compile "com.jakewharton:butterknife:$butterknifeVersion"
annotationProcessor "com.jakewharton:butterknife-compiler:$butterknifeVersion"
compile "com.squareup.retrofit2:retrofit:$retrofitVersion"
compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
compile 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
}
What did I miss? Thanks a lot.
Hi,
thx for providing this cookie jar :)
i had some troubles with the SerializableCookie, because it has no serialVersionUID definied.
Therefore, if something in the class is changed (or in my case i updated gradle) the uid changed and the cookies cannot be deserialized.
Furthermore, in SharedPrefsCookiePersistor.loadAll a 'null' cookie is added to the list which causes a NullPointerException later on:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.Cookie.name()' on a null object reference at com.franmontiel.persistentcookiejar.cache.IdentifiableCookie.hashCode(IdentifiableCookie.java:65) at java.util.Collections.secondaryHash(Collections.java:3427) at java.util.HashMap.put(HashMap.java:385) at java.util.HashSet.add(HashSet.java:95) at java.util.AbstractCollection.addAll(AbstractCollection.java:76) at com.franmontiel.persistentcookiejar.cache.SetCookieCache.updateCookies(SetCookieCache.java:46) at com.franmontiel.persistentcookiejar.cache.SetCookieCache.addAll(SetCookieCache.java:36) at com.franmontiel.persistentcookiejar.PersistentCookieJar.<init>(PersistentCookieJar.java:38)
So i suggest to maybe add private static final long serialVersionUID = 1L;
to SerializableCookie
and maybe something like this in SharedPrefsCookiePersistor.loadAll():
if (cookie != null) {
cookies.add(cookie);
} else {
//cannot decode cookies from sharedPrefs (serialize id changed e.g.); clear all
clear();
break;
}
Looking forward to hear your thoughts on this issue.
I use retrofit with okhttp. And following interceptor:
public class SaveCookiesInterceptor implements Interceptor {
private static final String TAG = "SaveCookiesInterceptor";
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
HashSet<String> cookies = new HashSet<>();
cookies.addAll(originalResponse.headers("Set-Cookie"));
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(App.context);
// if (sharedPreferences.getStringSet("cookies", null) == null) {
Log.d(TAG, "intercept: " + cookies.toString());
sharedPreferences.edit()
.putStringSet("cookies", cookies)
.apply();
// }
}
return originalResponse;
}
}
...
public class ReadCookiesInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
Set<String> preferences =
PreferenceManager.getDefaultSharedPreferences(App.context).getStringSet("cookies", new HashSet<String>());
for (String cookie : preferences) {
builder.addHeader("Cookie", cookie);
Log.d("OkHttp", "intercept read Header: " + cookie); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
}
return chain.proceed(builder.build());
}
}
...
instance.client = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.addInterceptor(new ReadCookiesInterceptor())
.addInterceptor(new SaveCookiesInterceptor())
.addInterceptor(interceptor).build();
Here is the problem:
I emulate login a website, it succeeds, but then I request my profile page, the response is the login page. After some trys, I add if (sharedPreferences.getStringSet("cookies", null) == null) {
in SaveCookiesInterceptor
. Then everthing works good.
So what happened in earth? Thx for your time in advance.
I'm trying to use this library, but I have never added anything to the android classpath except from gradle.
So please help by
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;`
Hello, when APP clear the background, restart again, cookie disappeared, and how should I do? Thank you very much!
It would be great if you could publish a sourcesJar through Jitpack so the sources are available in the IDE and at javadoc generation.
Here is an example build file with information on how to do that if you would like to: https://github.com/jitpack/android-example/blob/master/library/build.gradle
Hi,
thx again for the library :)
I added a clearSession method in PersistentCookieJar.. maybe also something useful for the lib.
public synchronized void clearSession() {
this.cache.clear();
this.cache.addAll(this.persistor.loadAll());
}
Cookie mi=a8f64fbd22f8f70afee8a48c2c18f202
This is my request cookie data.
@OverRide
synchronized public List loadForRequest(HttpUrl url) {
List cookiesToRemove = new ArrayList<>();
List validCookies = new ArrayList<>();
for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
Cookie currentCookie = it.next();
if (isCookieExpired(currentCookie)) {
cookiesToRemove.add(currentCookie);
it.remove();
} else if (currentCookie.matches(url)) {
validCookies.add(currentCookie);
}
}
persistor.removeAll(cookiesToRemove);
return validCookies;
}
I use your method for add request cookie. But there is only one param when I request server,and in the debug I had ensure the return validCookies is a unbroken cookie.
It gives me a headache,Have you met this situation???
Please help me!!!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.