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

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

问题描述

我在另一个项目中使用了 Jakarta commons HttpClient想要相同的 wire logging 输出,但使用标准"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 标头是由 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

A more complete solution that enables monitoring on all sockets can be found at http://www.javaspecialists.eu/archive/Issue169.html Thanks to Lawrence Dol for pointing in the right direction of using 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("
" + sb.toString());
                out.write(b, off, len);
            }

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

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

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

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