title: SelfSigned Cert for HTTPs download in Android Studio
body: <p>Ok so I have been working on downloading an image from an HTTPS website to display in my android app. I have been using the android dev site to do this. <a href="http://developer.android.com/training/articles/security-ssl.html" rel="nofollow">http://developer.android.com/training/articles/security-ssl.html</a>
  But, when I try to use the <code>.setSSLSocketFactory()</code> method the IDE says that it cannot resolve this method. I don't know what I am doing wrong here and I think that I have all the imports that I need to do this.
  Any help will be greatly appreciated.</p>
  
  <p>This is what I have for the code:</p>
  
  <pre><code>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;
  
  
  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();
          }
  
      }
  }
  </code></pre>