|
| 1 | +import okhttp3.Interceptor; |
| 2 | +import okhttp3.OkHttpClient; |
| 3 | +import okhttp3.Request; |
| 4 | +import okhttp3.Response; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.logging.Logger; |
| 8 | + |
| 9 | +public final class LoggingInterceptors { |
| 10 | + |
| 11 | + private static final Logger logger = Logger.getLogger(LoggingInterceptors.class.getName()); |
| 12 | + |
| 13 | + private final OkHttpClient client = new OkHttpClient.Builder() |
| 14 | +// .addInterceptor(new LoggingInterceptor()) |
| 15 | + .addInterceptor(new LoggingInterceptor2()) |
| 16 | + .addInterceptor(new LoggingInterceptor3()) |
| 17 | + .build(); |
| 18 | + |
| 19 | + public void run() throws Exception { |
| 20 | + Request request = new Request.Builder() |
| 21 | + .url("https://publicobject.com/helloworld.txt") |
| 22 | + .build(); |
| 23 | + |
| 24 | + Response response = client.newCall(request).execute(); |
| 25 | + response.body().close(); |
| 26 | + } |
| 27 | + |
| 28 | + private static class LoggingInterceptor implements Interceptor { |
| 29 | + @Override |
| 30 | + public Response intercept(Chain chain) throws IOException { |
| 31 | + long t1 = System.nanoTime(); |
| 32 | + Request request = chain.request(); |
| 33 | + logger.info(String.format("Sending request %s on %s%n%s", |
| 34 | + request.url(), chain.connection(), request.headers())); |
| 35 | + Response response = chain.proceed(request); |
| 36 | + |
| 37 | + long t2 = System.nanoTime(); |
| 38 | + logger.info(String.format("Received response for %s in %.1fms%n%s", |
| 39 | + request.url(), (t2 - t1) / 1e6d, response.headers())); |
| 40 | + return response; |
| 41 | + } |
| 42 | + } |
| 43 | + private static class LoggingInterceptor2 implements Interceptor { |
| 44 | + @Override |
| 45 | + public Response intercept(Chain chain) throws IOException { |
| 46 | + long t1 = System.nanoTime(); |
| 47 | + Request request = chain.request(); |
| 48 | + |
| 49 | + request =request.newBuilder().addHeader("request2","white2").build(); |
| 50 | + logger.info(String.format("Sending request %s on %s%n%s", |
| 51 | + request.url(), chain.connection(), request.headers())); |
| 52 | + |
| 53 | + |
| 54 | + Response response = chain.proceed(request); |
| 55 | + response.header("Accept-Ranges","black2"); |
| 56 | + long t2 = System.nanoTime(); |
| 57 | + logger.info(String.format("Received response for %s in %.1fms%n%s", |
| 58 | + request.url(), (t2 - t1) / 1e6d, response.headers())); |
| 59 | + return response; |
| 60 | + } |
| 61 | + } |
| 62 | + private static class LoggingInterceptor3 implements Interceptor { |
| 63 | + @Override |
| 64 | + public Response intercept(Chain chain) throws IOException { |
| 65 | + long t1 = System.nanoTime(); |
| 66 | + Request request = chain.request(); |
| 67 | + |
| 68 | + request =request.newBuilder().addHeader("request3","white3").build(); |
| 69 | + logger.info(String.format("Sending request %s on %s%n%s", |
| 70 | + request.url(), chain.connection(), request.headers())); |
| 71 | + |
| 72 | + Response response = chain.proceed(request); |
| 73 | + response.header("Accept-Ranges","black3"); |
| 74 | + long t2 = System.nanoTime(); |
| 75 | + logger.info(String.format("Received response for %s in %.1fms%n%s", |
| 76 | + request.url(), (t2 - t1) / 1e6d, response.headers())); |
| 77 | + return response; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String... args) throws Exception { |
| 82 | + new LoggingInterceptors().run(); |
| 83 | + } |
| 84 | +} |
0 commit comments