Not trusted certificate using ksoap2-android

前端 未结 4 1487
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 02:57

I\'m using ksoap2-android to make a call to wcf service over SSL. I can get it to work without SSL, but now I want to make the call over SSL, but I\'ve run in to some proble

4条回答
  •  爱一瞬间的悲伤
    2020-12-06 03:58

    Works for me KSOAP + Web service WCF with eclipse

    private static SoapObject getBody(final SoapSerializationEnvelope soapEnvelope) throws Exception {
            if (soapEnvelope.bodyIn == null) {
                throw new Exception("soapEnvelope.bodyIn=null");
            }
            else if (soapEnvelope.bodyIn.getClass() == SoapFault.class) {
                throw new ExceptionLogic((SoapFault) soapEnvelope.bodyIn));
            }
            else {
                return (SoapObject) soapEnvelope.bodyIn;
            }
    
        }
    
    private static SoapSerializationEnvelope sendRequete(final SoapObject soapReq, final String classMappingName,
                final Class classMapping, final int timeOutSpecial) {
    
    
    
            final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.implicitTypes = true;
            soapEnvelope.dotNet = true;
    
            if (classMappingName != null) {
                soapEnvelope.addMapping(NAMESPACE, classMappingName, classMapping);
            }
    
            soapEnvelope.setOutputSoapObject(soapReq);
    
            try {
    
                final HttpTransportSE httpTransport = new HttpTransportSE(Constante.urlWebService, timeOutSpecial);
                httpTransport.debug = BuildConfig.DEBUG;
    
                // Prod
                if (Constante.urlWebService.startsWith("https://")) {
                    final List headerList = new ArrayList();
                    headerList.add(new HeaderProperty("Authorization", "Basic "
                            + org.kobjects.base64.Base64.encode((Constante.CERTIFICAT_LOGIN + ":" + Constante.CERTIFICAT_MDP).getBytes())));
    
                    FakeX509TrustManager.allowAllSSL();
                    httpTransport.call(NAMESPACE + "/" + soapReq.getName(), soapEnvelope, headerList);
                }
                // Test
                else {
                    httpTransport.call(NAMESPACE + "/" + soapReq.getName(), soapEnvelope);
                }
    
                return soapEnvelope;
            }
            catch (final Exception e) {
                throw new Exception("Erreur : " + e.getMessage(), e);
            }
    
        }
    
    
    
        private static class FakeX509TrustManager implements X509TrustManager {
            private static TrustManager[] trustManagers;
            private final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
    
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return _AcceptedIssuers;
            }
    
            public static void allowAllSSL() {
                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
    
                    @Override
                    public boolean verify(final String hostname, final SSLSession session) {
                        return true;
                    }
                });
                SSLContext context = null;
                if (trustManagers == null) {
                    trustManagers = new TrustManager[] { new FakeX509TrustManager() };
                }
                try {
                    context = SSLContext.getInstance("TLS");
                    context.init(null, trustManagers, new SecureRandom());
                }
                catch (final NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
                catch (final KeyManagementException e) {
                    e.printStackTrace();
                }
                HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
            }
    
            @Override
            public void checkClientTrusted(final X509Certificate[] arg0, final String arg1) throws CertificateException {
    
            }
    
            @Override
            public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
    
            }
        }
    

提交回复
热议问题