You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
I use retrofit with okhttp. And following interceptor:
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) {
inSaveCookiesInterceptor
. Then everthing works good.So what happened in earth? Thx for your time in advance.
The text was updated successfully, but these errors were encountered: