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

May I ask you something #49

Open
DanteAndroid opened this issue Nov 21, 2017 · 0 comments
Open

May I ask you something #49

DanteAndroid opened this issue Nov 21, 2017 · 0 comments

Comments

@DanteAndroid
Copy link

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.

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

1 participant