监控okhttp的HTTP请求

作者: 我是吸血鬼 | 来源:发表于2018-11-06 13:50 被阅读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);
        }
    }

}

相关文章

网友评论

    本文标题:监控okhttp的HTTP请求

    本文链接:https://www.haomeiwen.com/subject/hycuxqtx.html