栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何启用Java HttpURLConnection通信的有线日志记录?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何启用Java HttpURLConnection通信的有线日志记录?

我已经能够记录所有SSL流量,并在默认端口之上实现自己的SSLSocketFactory。

这对我有用,因为我们所有的连接都使用HTTPS,并且可以使用HttpsURLConnection.setSSLSocketFactory方法设置套接字工厂。

可以在http://www.javaspecialists.eu/archive/Issue169.html上找到支持所有套接字的更完整解决方案。
感谢Lawrence
Dol
指出了使用Socket.setSocketImplFactory的正确方向。

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485755.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号