问题
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