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;
    }
}

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


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();
    }






}


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


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张航柯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值