How can I set SignalR in Android Studio to ignore SSL issues for delvelopement

前端 未结 1 1586

I have a site which uses SignalR. The code below (taken from SignalR-Chat example) connects to the production site with its verified SSL. It uses the jars from SignalR/java-

相关标签:
1条回答
  • 2020-12-03 09:45

    If just for development, I think you can refer the following code (available in SO):

    public class HttpsTrustManager implements X509TrustManager {
        private static TrustManager[] trustManagers;
        private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
    
        @Override
        public void checkClientTrusted(
            X509Certificate[] x509Certificates, String s)
        throws java.security.cert.CertificateException {}
    
        @Override
        public void checkServerTrusted(
            X509Certificate[] x509Certificates, String s)
        throws java.security.cert.CertificateException {}
    
        public boolean isClientTrusted(X509Certificate[] chain) {
            return true;
        }
    
        public boolean isServerTrusted(X509Certificate[] chain) {
            return true;
        }
    
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return _AcceptedIssuers;
        }
    
        public static void allowAllSSL() {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
    
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
    
            });
    
            SSLContext context = null;
            if (trustManagers == null) {
                trustManagers = new TrustManager[] {
                    new HttpsTrustManager()
                };
            }
    
            try {
                context = SSLContext.getInstance("TLS");
                context.init(null, trustManagers, new SecureRandom());
            } catch (NoSuchAlgorithmException | KeyManagementException e) {
                e.printStackTrace();
            }
    
            HttpsURLConnection.setDefaultSSLSocketFactory(context != null ? context.getSocketFactory() : null);
        }
    }
    

    Then in your Activity, call HttpsTrustManager.allowAllSSL(); before SignalR methods call.

    0 讨论(0)
提交回复
热议问题