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

CookiesStore.java 可能会有问题 #24

Open
xiaohangzhu opened this issue Apr 18, 2018 · 6 comments
Open

CookiesStore.java 可能会有问题 #24

xiaohangzhu opened this issue Apr 18, 2018 · 6 comments

Comments

@xiaohangzhu
Copy link

if (cookie.persistent()) {//需要持久化
//将cookies持久化到本地
prefsWriter.putString(url.host(), TextUtils.join(",", cookies.get(url.host()).keySet()));
prefsWriter.putString(name, encodeCookie(new OkHttpCookies(cookie)));
prefsWriter.apply();
} else {//不需要持久化
prefsWriter.remove(url.host());
prefsWriter.remove(name);
prefsWriter.apply();
}

这段代码,会导致所需的cookie 丢失

@xiaoyaoyou1212
Copy link
Owner

@xiaohangzhu 不太明白为啥会丢失所有cookie

@xiaohangzhu
Copy link
Author

你试一下,如果某个请求返回了 两个 cookie ,一个需要持久化,一个不需要持久化, 在不需要持久化的时候 执行(prefsWriter.remove(url.host());) 4849出问题了

@xiaohangzhu
Copy link
Author

因为 url.host() 是一样的,所以会导致 获取的时候 就找不到第一个需要持久化的 cookie 了

@xiaohangzhu
Copy link
Author

建议去掉 prefsWriter.remove(url.host()); 或者 直接把持久化的判断去掉就好,都持久化 一般也没啥问题吧

@xiaoyaoyou1212
Copy link
Owner

@xiaohangzhu 这个也不是都持久化就没问题,而是为啥会出现一个请求有两个cookie,就算都持久化也保存不了两个cookie,除非保存的Key做区分

@xiaohangzhu
Copy link
Author

为啥不会有两个cookie来? 两个cookie key-value 不一样啊,但是 url.host() 是一样的,本来这个持久化就是分开保存的,只是以url.host() 为索引 ,再去取对应 url的 cookie 值

持久化
prefsWriter.putString(url.host(), TextUtils.join(",", cookies.get(url.host()).keySet()));
prefsWriter.putString(name, encodeCookie(new OkHttpCookies(cookie)));
取值
Map<String, ?> prefsMap = cookiePrefs.getAll();
for (Map.Entry<String, ?> entry : prefsMap.entrySet()) {
String[] cookieNames = TextUtils.split((String) entry.getValue(), ",");
for (String name : cookieNames) {
String encodedCookie = cookiePrefs.getString(name, null);
if (encodedCookie != null) {
Cookie decodedCookie = decodeCookie(encodedCookie);
if (decodedCookie != null) {
if (!cookies.containsKey(entry.getKey())) {
cookies.put(entry.getKey(), new ConcurrentHashMap<String, Cookie>());
}
cookies.get(entry.getKey()).put(name, decodedCookie);
}
}
}
}

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

No branches or pull requests

2 participants