Getting error "No subject alternative names" when doing secure URL connection(针对处理https连接的安全异常)(转:http://www.coderanch.com/t/134

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:
view plain copy to clipboard print ?
  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.         }  
final String TEST_URL="https://www.verisign.com/"; URL url = new URL(TEST_URL); HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection(); httpsCon.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }); httpsCon.connect(); InputStream is = httpsCon.getInputStream(); int nread = 0; byte [] buf = new byte[8192]; while ((nread = is.read(buf)) != -1) { System.out.write(buf, 0, nread); }

Nice to meet you.

你可能感兴趣的:(其它)