0

I check the certificate which is stored in app side and server side both are matching or not only trusted certificate should work

Below is my code and that code I am getting error in setDefaultSSLSocketFactory

       CertificateFactory cf = CertificateFactory.getInstance("X.509");

        InputStream caInput = new BufferedInputStream(context.getResources().openRawResource(R.raw.cert));
        Certificate ca = cf.generateCertificate(caInput);
        caInput.close();

        // Create a KeyStore containing our trusted CAs
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", ca);

        // Create a TrustManager that trusts the CAs in our KeyStore
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);

        // Create an SSLContext that uses our TrustManager
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);

        urlConnection = (HttpURLConnection) url.openConnection();

     (HttpsURLConnection)urlConnection).setDefaultSSLSocketFactory(sslContext.getSocketFactory());

        urlConnection.setRequestMethod(params[1]);   //POST or GET

        urlConnection.setRequestProperty("Content-type", "application/json");
        urlConnection.setRequestProperty("Accept", "application/json");

        urlConnection.setConnectTimeout(Constant.TIMEOUT_CONNECTION);

        urlConnection.setRequestProperty("Authorization", basicAuth);//auth value

setDefaultSSLSocketFactory I am getting error cannot resolve method; where am I am going wrong? I have check the certificate both side certificate which is used in url and certificate which I am having but in setDefaultSSLSocketFactory is showing error so I cannot avail to test.

Update

I resolve the issue of cannot resolve method setDefaultSSl but now I am getting exception: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

halfer
  • 19,824
  • 17
  • 99
  • 186
Atul Dhanuka
  • 1,453
  • 5
  • 20
  • 56
  • Post the error log here, So we can help better. – Ronak Joshi Feb 16 '18 at 07:28
  • @RonakJoshi hi i resolve cannot resolve method issue but i am getting exception javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. – Atul Dhanuka Feb 16 '18 at 07:38
  • have a look here. https://stackoverflow.com/questions/42468807/javax-net-ssl-sslexception-ssl-handshake-aborted-on-android-old-devices/42471738#42471738 This might help you – Arshad Feb 16 '18 at 09:56

0 Answers0