Https Connection Android

后端 未结 15 1721
萌比男神i
萌比男神i 2020-11-22 04:43

I am doing a https post and I\'m getting an exception of ssl exception Not trusted server certificate. If i do normal http it is working perfectly fine. Do I have to accept

15条回答
  •  情话喂你
    2020-11-22 05:25

    I make this class and found

    package com.example.fakessl;
    
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.SSLSession;
    import javax.net.ssl.TrustManager;
    
    import android.util.Log;
    
    public class CertificadoAceptar {
        private static TrustManager[] trustManagers;
    
        public static class _FakeX509TrustManager implements
                javax.net.ssl.X509TrustManager {
            private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
    
            public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                    throws CertificateException {
            }
    
            public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                    throws CertificateException {
            }
    
            public boolean isClientTrusted(X509Certificate[] chain) {
                return (true);
            }
    
            public boolean isServerTrusted(X509Certificate[] chain) {
                return (true);
            }
    
            public X509Certificate[] getAcceptedIssuers() {
                return (_AcceptedIssuers);
            }
        }
    
        public static void allowAllSSL() {
    
            javax.net.ssl.HttpsURLConnection
                    .setDefaultHostnameVerifier(new HostnameVerifier() {
                        public boolean verify(String hostname, SSLSession session) {
                            return true;
                        }
                    });
    
            javax.net.ssl.SSLContext context = null;
    
            if (trustManagers == null) {
                trustManagers = new javax.net.ssl.TrustManager[] { new _FakeX509TrustManager() };
            }
    
            try {
                context = javax.net.ssl.SSLContext.getInstance("TLS");
                context.init(null, trustManagers, new SecureRandom());
            } catch (NoSuchAlgorithmException e) {
                Log.e("allowAllSSL", e.toString());
            } catch (KeyManagementException e) {
                Log.e("allowAllSSL", e.toString());
            }
            javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(context
                    .getSocketFactory());
        }
    }
    

    in you code white this

    CertificadoAceptar ca = new CertificadoAceptar();
    ca.allowAllSSL();
    HttpsTransportSE Transport = new HttpsTransportSE("iphost or host name", 8080, "/WS/wsexample.asmx?WSDL", 30000);
    

提交回复
热议问题