How to enable SSL debugging on the Android platform?

前端 未结 4 1112
北海茫月
北海茫月 2021-02-04 04:59

Is there something similar to setting -D javax.net.debug=ssl at the command line for Java desktop applications, but for the Android? I\'ve tried setting it in code

4条回答
  •  爱一瞬间的悲伤
    2021-02-04 05:30

    you can write a TrustManager class to handle it. example :

    ClientConnectionManager cm = new BasicClientConnectionManager();
    cm.getSchemeRegistry().register(createHttpsScheme());
    DefaultHttpClient client = new DefaultHttpClient(cm);
    String url = "https://your domain/your url";
    HttpGet get = new HttpGet(url);
    HttpResponse resp = client.execute(get);
    
    etc..
    
    public static Scheme createHttpsScheme() {
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new TrustManager[] {
                    new TestTrustManager()
            }, new SecureRandom());
    
            SSLSocketFactory sf = new SSLSocketFactory(context);
            return new Scheme("https", 443, sf);
    }
    

    int TestTrustManager.java you can print the chain like this:

    public class TestTrustManager implements X509TrustManager {
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    
           for (int i = 0; i < chain.length; ++i) {
            System.out.println(chain[i]);
           }
    
           decorated.checkServerTrusted(chain, authType);
      }
    }
    

提交回复
热议问题