Skip to content

Instantly share code, notes, and snippets.

@ParkSangGwon
Last active January 12, 2016 12:27
Show Gist options
  • Save ParkSangGwon/ceb7d5c58b030516b9c5 to your computer and use it in GitHub Desktop.
Save ParkSangGwon/ceb7d5c58b030516b9c5 to your computer and use it in GitHub Desktop.
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