title: No subject alternative DNS name matching
body: <p>I am trying to perform POST request in Java. I have succeded with HTTP, but HTTPS is tricky for me.
  I am using this code:</p>
  
  <pre><code>// Create a trust manager that does not validate certificate chains
  TrustManager[] trustAllCerts = new TrustManager[]{
          new X509TrustManager() {
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                  return null;
              }
              public void checkClientTrusted(
                      java.security.cert.X509Certificate[] certs, String authType) {
              }
              public void checkServerTrusted(
                      java.security.cert.X509Certificate[] certs, String authType) {
              }
          }
  };
  
  // Install the all-trusting trust manager
  try {
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(null, trustAllCerts, new java.security.SecureRandom());
      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  } catch (Exception e) {
  }
  
  
  String httpsURL = "https://requestb.in/191g0961";
  
  String query = "email="+URLEncoder.encode("abc@xyz.com","UTF-8"); 
  query += "&amp;";
  query += "password="+URLEncoder.encode("abcd","UTF-8") ;
  
  URL myurl = new URL(httpsURL);
  HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
  con.setRequestMethod("POST");
  
  con.setRequestProperty("Content-length", String.valueOf(query.length())); 
  con.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
  con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)"); 
  con.setDoOutput(true); 
  con.setDoInput(true); 
  
  DataOutputStream output = new DataOutputStream(con.getOutputStream());  
  
  output.writeBytes(query);
  
  output.close();
  
  DataInputStream input = new DataInputStream(con.getInputStream()); 
  
  
  for( int c = input.read(); c != -1; c = input.read() ) 
      System.out.print( (char)c ); 
  input.close(); 
  
  System.out.println("Resp Code:"+con .getResponseCode()); 
  System.out.println("Resp Message:"+ con .getResponseMessage());
  </code></pre>
  
  <p>I have added TrustManager in hope it solves my problem, because when I load my testing webpage <a href="https://requestb.in/191g0961" rel="nofollow">https://requestb.in/191g0961</a> in Firefox, it says that site uses untrusted certificate, etc..</p>
  
  <p>My code works with pages that are loaded in Firefox without any warnings, like this page: <a href="https://extranet.nix.cz/login" rel="nofollow">https://extranet.nix.cz/login</a> (you can try it)</p>
  
  <p>But with <a href="https://requestb.in/191g0961" rel="nofollow">https://requestb.in/191g0961</a> it is giving me following exceptions:</p>
  
  <pre><code>Exception in thread "main" javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching requestb.in found.
  at sun.security.ssl.Alerts.getSSLException(Unknown Source)
  at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
  at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
  at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
  at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
  at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
  at sun.security.ssl.Handshaker.processLoop(Unknown Source)
  at sun.security.ssl.Handshaker.process_record(Unknown Source)
  at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
  at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
  at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
  at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
  at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
  at https.Sender.main(Sender.java:80)
  Caused by: java.security.cert.CertificateException: No subject alternative DNS name matching requestb.in found.
  at sun.security.util.HostnameChecker.matchDNS(Unknown Source)
  at sun.security.util.HostnameChecker.match(Unknown Source)
  at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
  at sun.security.ssl.AbstractTrustManagerWrapper.checkAdditionalTrust(Unknown Source)
  at sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(Unknown Source)
  ... 13 more
  </code></pre>
  
  <p>Have you any ideas how to solve this?
  Thanks in advance!</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>I have created this simple class:</p>
  
  <pre><code>public class Verifier implements HostnameVerifier {
  
      public boolean verify(String arg0, SSLSession arg1) {
              return true;   // mark everything as verified
      }
  }
  </code></pre>
  
  <p>and then I added:</p>
  
  <pre><code>con.setHostnameVerifier(new Verifier());
  </code></pre>