http日志打印

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

1、创建URLStreamHandlerFactory

java 复制代码
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;
    }
}

2、创建URLStreamHandler

java 复制代码
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);
    }
}

3、创建HttpURLConnection

java 复制代码
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();
    }






}

4、静态代码块

java 复制代码
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);
    }


}
相关推荐
律队i2 小时前
【计算机网络】五层对比,物理设备对比
网络·计算机网络
raysync8882 小时前
镭速大文件传输软件向金融银行的文档管理提供高效的解决方案
服务器·网络·金融
.Ayang3 小时前
文件上传漏洞
网络·计算机网络·安全·web安全·网络安全·系统安全·网络攻击模型
Michael_Good3 小时前
【计算机网络】网卡NIC的工作内容包括哪些呢?
网络·计算机网络
华普微HOPERF3 小时前
Matter1.4重磅来袭,智能家居进入“互联”新纪元
网络·智能路由器·智能家居
weixin_442643423 小时前
FileLink跨网文件安全摆渡系统——企业数据流转的安全桥梁
大数据·网络·安全·filelink文件摆渡系统
pumpkin845145 小时前
客户端发送http请求进行流量控制
python·网络协议·http
HUODUNYUN6 小时前
小程序免备案
网络·web安全·小程序·1024程序员节
速盾cdn6 小时前
速盾:如何有效防止服务器遭受攻击?
网络·安全·web安全
华纳云IDC服务商6 小时前
怎么选择香港服务器的线路?解决方案
服务器·网络·香港服务器