title: Consume REST API using java - SSLHandshakeException
body: <p>I am trying to consume a REST API using Java. however, when I run the code it shows me this exception.
  I didn't understand what does it mean, I try to search for the solution but I can't get it :(</p>
  
  <p>this is the connection code:</p>
  
  <pre><code>import javax.ws.rs.core.MediaType;
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import javax.ws.rs.client.Client;
  import javax.ws.rs.client.ClientBuilder;
  import javax.ws.rs.client.Invocation;
  import javax.ws.rs.client.WebTarget;
  import org.glassfish.jersey.client.ClientConfig;
  import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
  
  public class connectToAPI {
           public static void main(String[] args) {
  
  try{
  URL url = new URL("https://ip:port/rest/path");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  String userpassword = "user:pass";
  conn.setUseCaches(false);
  conn.setDoInput(true);
  conn.setDoOutput(true);
  conn.setRequestMethod("POST");
  conn.setRequestProperty("Authorization","Basic "+
                userpassword);
  conn.setRequestProperty("Content-Type","application/json");
  OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  wr.flush();
  int status = 0;
  if( null != conn ){
  status = conn.getResponseCode();
  }
  
  if( status != 0){
         System.out.println("status!=0");
  if( status == 200 ){
  
         System.out.println("status==200");
  //SUCCESS message
  BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  
  
  
  }else if(status == 401){
  
         System.out.println("status==401");
  
  
  
  }else if(status == 501){
  
         System.out.println("status==501");
  
  
  
  }else if( status == 503){
  
         System.out.println("status==503");
  
  
  
  }
  
  
  
  
  
  }
  
  
  
  } catch (Exception e) {
  
  e.printStackTrace();
  
  }
  
  }
                }
  </code></pre>
  
  <p>when I run the code it shows me this exception:</p>
  
  <blockquote>
    <p>javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names matching IP address ip found</p>
  
  <pre><code>   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.getOutputStream0(Unknown Source)
  
     at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
  
     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
  
     at alert.connectToBAM.main(connectToBAM.java:77)
  </code></pre>
    
    <p>Caused by: java.security.cert.CertificateException: No subject
    alternative names matching IP address ip found</p>
  
  <pre><code>   at sun.security.util.HostnameChecker.matchIP(Unknown Source)
  
     at sun.security.util.HostnameChecker.match(Unknown Source)
  
     at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
  
     at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
  
     at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
  
     at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
  
     ... 14 more
  </code></pre>
  </blockquote>
  
  <p>I also tried jersey clinet:</p>
  
  <pre><code>javax.ws.rs.client.Client client = ClientBuilder.newClient();
  
                             WebTarget target = client.target("https://ip:port/rest/path");
  
                             System.out.println(
  
                                           target.request(MediaType.APPLICATION_JSON).get(String.class));
  </code></pre>
  
  <p>it shows this error:</p>
  
  <blockquote>
    <p>Exception in thread "main" javax.ws.rs.ProcessingException:
    javax.net.ssl.SSLHandshakeException:
    java.security.cert.CertificateException: No subject alternative names
    matching IP address ip found</p>
  
  <pre><code>   at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:284)
  
     at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:278)
  
     at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$1(JerseyInvocation.java:767)
  
     at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
  
     at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
  
     at org.glassfish.jersey.internal.Errors.process(Errors.java:229)
  
     at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:414)
  
     at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:765)
  
     at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:428)
  
     at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:324)
  
     at alert.connectToBAM.main(connectToBAM.java:47)
  </code></pre>
  </blockquote>
  
  <p>Any idea? pleaseeee, it is my graduation project :(</p>