解决java.io.IOException: HTTPS hostname wrong: should be


 原因:当访问HTTPS的网址。您可能已经安装了服务器证书到您的JRE的keystore 。但这个错误是指服务器的名称与证书实际域名不相等。这通常发生在你使用的是非标准网上签发的证书。 

 解决方法:让JRE相信所有的证书和对系统的域名和证书域名。以下是一小段代码,可以用来实现这一目标

public class Servlet_test {

 public static void main(String[] args)throws Exception {
  URL url=new URL("https://localhost:8443/sso/servlet/SyncServletmethod=deleteOrg&appid=ec28d8fd22cf4bdf0122cf53e8a10002&orgcoding=001311&memo=");
  HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
  conn.setHostnameVerifier(new Servlet_test().new TrustAnyHostnameVerifier());
  conn.connect();
  InputStream ip= conn.getInputStream();
  BufferedReader br=new BufferedReader(new InputStreamReader(ip));
  String line;
  StringBuffer strb = new StringBuffer();
  while ((line = br.readLine()) != null) {
   strb.append(line);
  }
  String ss = strb.toString();
  System.out.println(ss);
 }d

 public class TrustAnyHostnameVerifier implements HostnameVerifier {
  public boolean verify(String hostname, SSLSession session) {
   // 直接返回true
   return true;
  }
 }
 
}
如果你为服务器证书经常改变,而自己的客户端方也跟随改变而头痛的话,以上方法也适合。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM