-
Notifications
You must be signed in to change notification settings - Fork 245
Open
Description
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.
Metadata
Metadata
Assignees
Labels
No labels