• http日志打印


    http请求老是有日志打印如何全局配置输出

    1、创建URLStreamHandlerFactory

    package com.zhk.study.test;
    
    
    import java.net.URLStreamHandler;
    import java.net.URLStreamHandlerFactory;
    
    class MyProtocolHandlerFactory implements URLStreamHandlerFactory {
        @Override
        public URLStreamHandler createURLStreamHandler(String protocol) {
            if (protocol.contains("http")) {
                return new MyProtocolHandler();
            }
            return null;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2、创建URLStreamHandler

    package com.zhk.study.test;
    
    
    import java.io.IOException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLStreamHandler;
    
    public class MyProtocolHandler extends URLStreamHandler {
    
    
    
        @Override
        protected URLConnection openConnection(URL u) throws IOException {
            return new CustomURLConnection(u);
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3、创建HttpURLConnection

    package com.zhk.study.test;
    
    
    
    import java.io.*;
    import java.lang.reflect.Method;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class CustomURLConnection extends HttpURLConnection {
        private HttpURLConnection urlConnection;
    
    
        private ThreadLocal<InputStream> threadLocal = new ThreadLocal();
    
    
        private static String PREFIX = "sun.net.www.protocol";
    
    
        public static OutputStream postParams = null;
    
    
        public CustomURLConnection(URL url) {
            super(null);
            String name = PREFIX + "." + url.getProtocol() + ".Handler";
            try {
                Class<?> aClass = Class.forName(name);
                Method method = aClass.getDeclaredMethod("openConnection", URL.class);
                Object handler = aClass.getConstructor().newInstance();
                method.setAccessible(true);
                this.urlConnection = (HttpURLConnection)method.invoke(handler, url);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    
        @Override
        public void disconnect() {
    
            InputStream inputStream = threadLocal.get();
            if (inputStream!=null) {
                try{
                    inputStream.close();
                }catch (Exception e) {
                }
                try {
                    threadLocal.remove();
                } catch (Exception e) {
                }
            }
    
            if (postParams != null) {
                System.out.println("参数:"+ postParams.toString());
                try{
                    postParams.close();
                } catch (Exception e) {
    
                }
    
            }
            this.urlConnection.disconnect();
        }
    
        @Override
        public boolean usingProxy() {
            return this.urlConnection.usingProxy();
        }
    
        @Override
        public void connect() throws IOException {
            this.urlConnection.connect();
        }
    
    
    
        @Override
        public InputStream getInputStream() throws IOException {
            try(
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    
            ) {
                if (threadLocal.get() == null) {
                    InputStream inputStream = urlConnection.getInputStream();
                    // 读取响应数据并缓存到一个字节数组
                    int bytesRead;
                    byte[] data = new byte[1024];
    
                    while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
                        buffer.write(data, 0, bytesRead);
                    }
                    System.out.println("日志" + buffer);
                    // 获取响应数据的字节数组
                    byte[] responseData = buffer.toByteArray();
                    // 创建一个新的 ByteArrayInputStream 来读取响应数据
                    ByteArrayInputStream responseStream = new ByteArrayInputStream(responseData);
                    threadLocal.set(responseStream);
                }
            } catch (Exception e) {
    
            }
    
            return threadLocal.get();
    
        }
    
    
    
    
        @Override
        public void setDoInput(boolean doinput) {
            this.urlConnection.setDoInput(doinput);
        }
        @Override
        public void setDoOutput(boolean doinput) {
            this.urlConnection.setDoOutput(doinput);
        }
    
        @Override
        public OutputStream getOutputStream() throws IOException {
            postParams = urlConnection.getOutputStream();
            return this.urlConnection.getOutputStream();
        }
    
    
    
    
    
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131

    4、静态代码块

    package com.zhk.study.test;
    
    import cn.hutool.http.HttpRequest;
    import cn.hutool.http.HttpResponse;
    import cn.hutool.http.HttpUtil;
    
    
    import java.net.URL;
    
    public class Test {
        static {
            URL.setURLStreamHandlerFactory(new MyProtocolHandlerFactory());
        }
        public static void main(String[] args) {
    
            HttpRequest get = HttpUtil.createPost("https://xxxx/getCard");
            get.form("vin","0ZZD3333");
            get.body("{\"name\":123}");
            HttpResponse execute = get.execute();
            String body = execute.body();
            System.out.println("我是结果集:::" + body);
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    LiveGBS流媒体平台GB/T28181常见问题-如何禁用删除已注册设备国标设备如何删除
    【自然语言处理】基于python的问答系统实现
    U盘显示无媒体怎么办?方法很简单
    《第一堂棒球课》:王牌三垒手·棒球5号位
    [mysql]游标和触发器
    SQL Server对象类型(4)——4.4.索引视图(Indexed View)
    Go 语言内置类型全解析:从布尔到字符串的全维度探究
    2021最新中高级Java面试题目,Java面试题汇总
    剑指offer57-61排序-堆
    cy.visit 执行逻辑的单步调试
  • 原文地址:https://blog.csdn.net/weixin_49390750/article/details/132826656