监控okhttp的HTTP请求

任何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);
        }
    }

}

    原文作者:我是吸血鬼
    原文地址: https://www.jianshu.com/p/ef6a3d9bcfe4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞