Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class AddCookiesInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
// Preference에서 cookies를 가져오는 작업을 수행
Set<String> preferences = SharedPreferenceBase.getSharedPreference(APIPreferences.SHARED_PREFERENCE_NAME_COOKIE, new HashSet<String>());
for (String cookie : preferences) {
builder.addHeader("Cookie", cookie);
}
// Web,Android,iOS 구분을 위해 User-Agent세팅
builder.removeHeader("User-Agent").addHeader("User-Agent", "Android");
return chain.proceed(builder.build());
}
}
public class ReceivedCookiesInterceptor implements Interceptor {
@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<>();
for (String header : originalResponse.headers("Set-Cookie")) {
cookies.add(header);
}
// Preference에 cookies를 넣어주는 작업을 수행
SharedPreferenceBase.putSharedPreference(APIPreferences.SHARED_PREFERENCE_NAME_COOKIE, cookies);
}
return originalResponse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment