Getting error "No subject alternative names" when doing secure URL connection

 

 

I believe that, by default, the HTTPS URL classes closely follow the rules laid out for checking server identity in section 3.1 of RFC 2818. I believe the error message you recieve indicates those identity checks fail. In general, if you specify a URL of "https://www.server.net", then the certificate sent back from that server should contain a special field called the "Subject Alternative Name", and furthermore the value of this field should be www.server.net. In lieu of this, it may contain www.server.net in one of the CN fields of the subject name. But please see RFC 2818 for the actual rules. If neither of these is true, I think you receive the error you got.

To see if this is indeed the problem, you can temporarily disable the hostname check by using a custom hostname verifier that always returns true. NOTE: this is just for testing, don't do this in your real application! Here is a small example showing this:
  1.         final String TEST_URL="https://www.verisign.com/";   
  2.         URL url = new URL(TEST_URL);   
  3.         HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection();   
  4.         httpsCon.setHostnameVerifier(new HostnameVerifier()   
  5.         {         
  6.             public boolean verify(String hostname, SSLSession session)   
  7.             {   
  8.                 return true;   
  9.             }   
  10.         });   
  11.         httpsCon.connect();   
  12.         InputStream is = httpsCon.getInputStream();   
  13.         int nread = 0;   
  14.         byte [] buf = new byte[8192];   
  15.         while ((nread = is.read(buf)) != -1)   
  16.         {   
  17.             System.out.write(buf, 0, nread);   
  18.         }  

Nice to meet you.

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐