Android开发Android面试基础Android技术知识

监控okhttp的HTTP请求

2018-11-06  本文已影响12人  我是吸血鬼

任何App上线应该杜绝http请求的存在。客户端可以通过AOP非侵入式监控所有的请求链接,从而实现非HTTPS请求链接的监控、上报、BUG修复。

@Aspect
public class OkHttpClientAop {

    @Pointcut("execution(okhttp3.OkHttpClient.Builder.new(..))")
    public void builder() {
    }

    @After("builder()")
    public void newBuilderAfter(JoinPoint joinPoint) {
        OkHttpClient.Builder builder = (OkHttpClient.Builder) joinPoint.getTarget();
        builder.addInterceptor(new HttpsMonitor());
    }

    static class HttpsMonitor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            String rawUrl = request.url().uri().toString();
            StringBuilder sb = new StringBuilder("\u21E0 ");
            sb.append("isHttps:").append(request.url().isHttps());
            sb.append(";rawUrl:").append(rawUrl);

            if (!request.url().isHttps()) {
                Log.e("demon", sb.toString());
            } else {
                Log.e("demon", sb.toString());
            }

            return chain.proceed(request);
        }
    }

}

上一篇下一篇

猜你喜欢

热点阅读