How to enable or implement logging for HttpsURLConnection traffic on Android? [closed]

感情迁移 提交于 2019-12-12 03:57:28

问题


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)?


回答1:


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.

Simple non-working solution

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.

By default, it directs output to System.err.

So I redirected output to logcat with

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

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.



来源:https://stackoverflow.com/questions/15536252/how-to-enable-or-implement-logging-for-httpsurlconnection-traffic-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!