代码之家  ›  专栏  ›  技术社区  ›  Punter Vicky

setDefaultHostnameVerifier中验证方法的用途

  •  -1
  • Punter Vicky  · 技术社区  · 7 年前

    你能告诉我下面的代码是什么吗?从哪里获取主机名?verify方法的目的是什么?

    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
                new javax.net.ssl.HostnameVerifier(){
    
                    public boolean verify(String hostname,
                                          javax.net.ssl.SSLSession sslSession) {
                        System.out.println(" hostname = " + hostname);
                        if (hostname.equals("localhost")) {
                            return true;
                        }
                        return false;
                    }
                });
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Ranjith    7 年前

    当您连接到HTTPS URL时,目标站点将显示其证书,其中包含可使用证书的域列表(参见下面的示例图像)。当您获得 HttpsURLConnection

    HostnameVerifier 当上述检查失败时调用。如果要连接的域是localhost,则允许继续进行连接设置。这使您可以使用任何证书的服务在 https://localhost

    enter image description here