如何为java HttpURLConnection流量启用线路记录? [英] How to enable wire logging for a java HttpURLConnection traffic?

查看:101
本文介绍了如何为java HttpURLConnection流量启用线路记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个项目中使用了 Jakarta commons HttpClient 想要使用标准HttpUrlConnection进行相同的有线记录输出。

I've used Jakarta commons HttpClient in another project and I would like the same wire logging output but using the "standard" HttpUrlConnection.

我使用 Fiddler 作为代理,但我想直接从java记录流量。

I've used Fiddler as a proxy but I would like to log the traffic directly from java.

捕获连接输入和输出流的内容是不够的,因为HTTP标头是由HTTP头编写和使用的HttpUrlConnection类,所以我将无法记录标题。

Capturing what goes by the connection input and output streams is not enough because the HTTP headers are written and consumed by the HttpUrlConnection class, so I will not be able to log the headers.

推荐答案

我已经能够记录所有SSL流量在顶部实现我自己的 SSLSocketFactory 默认的一个。

I've been able to log all SSL traffic implementing my own SSLSocketFactory on top of the default one.

这对我有用,因为我们所有的连接都使用HTTPS,我们可以使用方法 HttpsURLConnection.setSSLSocketFactory

This worked for me because all of our connections are using HTTPS and we can set the socket factory with the method HttpsURLConnection.setSSLSocketFactory.

可以在 http://www.javaspecialists.eu/archive/Issue169.html
感谢 Lawrence Dol 指向使用 Socket.setSocketImplFactory

这是我尚未准备好生产代码:

Here is my not ready for production code:

public class WireLogSSLSocketFactory extends SSLSocketFactory {

    private SSLSocketFactory delegate;

    public WireLogSSLSocketFactory(SSLSocketFactory sf0) {
        this.delegate = sf0;
    }

    public Socket createSocket(Socket s, String host, int port,
            boolean autoClose) throws IOException {
        return new WireLogSocket((SSLSocket) delegate.createSocket(s, host, port, autoClose));
    }

    /*
    ...
    */

    private static class WireLogSocket extends SSLSocket {

        private SSLSocket delegate;

        public WireLogSocket(SSLSocket s) {
            this.delegate = s;
        }

        public OutputStream getOutputStream() throws IOException {
            return new LoggingOutputStream(delegate.getOutputStream());
        }

        /*
        ...
        */

        private static class LoggingOutputStream extends FilterOutputStream {
            private static final Logger logger = Logger.getLogger(WireLogSocket.LoggingOutputStream.class);
            //I'm using a fixed charset because my app always uses the same. 
            private static final String CHARSET = "ISO-8859-1";
            private StringBuffer sb = new StringBuffer();

            public LoggingOutputStream(OutputStream out) {
                super(out);
            }

            public void write(byte[] b, int off, int len)
                    throws IOException {
                sb.append(new String(b, off, len, CHARSET));
                logger.info("\n" + sb.toString());
                out.write(b, off, len);
            }

            public void write(int b) throws IOException {
                sb.append(b);
                logger.info("\n" + sb.toString());
                out.write(b);
            }

            public void close() throws IOException {
                logger.info("\n" + sb.toString());
                super.close();
            }
        }
    }
}

这篇关于如何为java HttpURLConnection流量启用线路记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆