title: Where SSLSocketFactory and HttpsURLConnection work diffrerently?
body: <p>I have got a server with self-signed certificate. I've imported it with a keytool on my computer and use</p>
  
  <pre><code>-Djavax.net.ssl.trustStore=blabla
  </code></pre>
  
  <p>compile argument. When I try to run the following code:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
  SSLSocket socket = (SSLSocket) factory.createSocket("MY_URL_DIGITS", 443);
  OutputStream os = socket.getOutputStream();
  os.write("Test request \n".getBytes());
  os.flush();
  os.close();
  </code></pre>
  
  <p>Everything goes alright and I can see the "Test request" on the server. However, when I run:</p>
  
  <pre class="lang-java prettyprint-override"><code>URL url = new URL("https://MY_URL_DIGITS");
  HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
  OutputStream os = con.getOutputStream();
  os.write("Test request \n".getBytes());
  os.flush();
  os.close();
  </code></pre>
  
  <p>I've got the </p>
  
  <pre class="lang-java prettyprint-override"><code>javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present
  </code></pre>
  
  <p>So what is the principal difference between these two snippets?</p>