• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps HTTPS with self signed cert

HeavensSentSword

Well-Known Member
So i have been trying to use the android dev site's tutorial on how to use a custom trust manager to allow for connection to a self-signed server. This one https://developer.android.com/training/articles/security-ssl.html
But when I write this line
urlConnection.setSSLSocketFactory(context.getSocketFactory());
it says that it cannot resolve the method setSSLSocketFactory();
I think I have all the libs I need imported but I dont know what is going wrong here.
Thank you for any help with this.
Here is what I have so far.

Java:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

/**
* Created by MMILLAR on 2/8/2016.
*/
public class CertificateHandler {
    private final String myCert = "MyCA.crt";

    public void getToken(HttpURLConnection urlConnection) {

                  try {
                // Load CAs from an InputStream
                // (could be from a resource or ByteArrayInputStream or ...)
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                // From https://www.washington.edu/itconnect/security/ca/load-der.crt
                InputStream caInput = new BufferedInputStream(new FileInputStream("myCert"));
                Certificate ca;
                try {
                    ca = cf.generateCertificate(caInput);
                    System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
                } finally {
                    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 context = SSLContext.getInstance("TLS");
                context.init(null, tmf.getTrustManagers(), null);
                urlConnection.setSSLSocketFactory(context.getSocketFactory());


        }catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }

    }
}
 
Ok I found my issue after looking at it a lot.
I was using URLHttpConnection and I needed URLHttpsConnection.
Yes the s is all that I was missing.
 
Back
Top Bottom