如何启用或实现在Android上的HttpsURLConnection的流量日志? [英] How to enable or implement logging for HttpsURLConnection traffic on Android?

查看:294
本文介绍了如何启用或实现在Android上的HttpsURLConnection的流量日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够登录(至少)报头和加密发送的消息(可选)体/接受了我的申请使用HttpsURLConnection的。这应该是类似这个

I need to be able to log (at least) headers and (optionally) body of unencrypted messages sent / received using HttpsURLConnection in my application. This should be similar to this.

有一个简单的办法做到这一点(例如设置一些标志来启用它)?

Is there a simple way to do it (e.g. set some flags to enable it)?

推荐答案

实现的自定义SSLSocketFactory的和SSLSocket的子类,采用委托者类从这里。委托扩展到在超了层次执行方法查找。

Complex working solution

Implemented custom SSLSocketFactory and subclass of SSLSocket, using Delegator class from here. Delegator was extended to perform method lookup in superclasses up the hierarchy.

您可以从下载演示项目这里,看看它是如何工作的。

You may download demo project from here and see how it works.

SSL层实现包含自己的日志code,使用org.apache.harmony.xnet.provider.jsse.Logger类。它读取的距离,如果定义,可能包含的值conn_state,PRF,机警,ssocket,记录引擎和插座逗号分隔列表JSSE系统属性参数的名称。

SSL layer implementation contains own logging code, that uses org.apache.harmony.xnet.provider.jsse.Logger class. It reads names of parameters from jsse system property that, if defined, may contain comma-separated list of values conn_state, prf, alert, ssocket, record, engine and socket.

在默认情况下,它的输出定向到System.err的。

By default, it directs output to System.err.

所以我重定向输出到LogCat中

So I redirected output to logcat with

$ adb shell stop
$ adb shell setprop log.redirect-stdio true
$ adb shell start

但(我认为)没有它由Logger类之前初始化JSSE系统属性。三个选项:与亚行的shell setprop设置,创建/data/local.prop和System.setProperty()在MainActivity类别的静态初始化设置,后来才选择成功(至少的getProperty()之后返回设定值)。

but (I think) failed to initialize jsse system property before it is used by the Logger class. Of three options: set it with adb shell setprop, create /data/local.prop and set with System.setProperty() in static initializer of MainActivity class, only later option succeeded (at least getProperty() after it returned set value).

请,我们知道,如果你用第二个方法成功了。

Please, let know, if you succeeded with second approach.

这篇关于如何启用或实现在Android上的HttpsURLConnection的流量日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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