尝试使用https连接到服务器时,我收到此'HTTPS hostname wrong:'错误。我的网址看起来像这样

https://sub.domain.com/tamnode/webapps/app/servlet.

我使用以下代码连接

    // Create a URLConnection object for a URL
    URL url = new URL(requestedURL);
    HttpURLConnection.setFollowRedirects(false);

    // connect
    connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$

    OutputStreamWriter wr = new OutputStreamWriter(connection
            .getOutputStream());

但后来出错了

IOException: HTTPS hostname wrong:  should be <sub.domain.com>. 
    at sun.net.www.protocol.https.HttpsClient.checkURLSpoofing
    ....

这是过去曾经工作但不再有效的代码。系统架构发生了一些变化,但我需要在接近负责人之前获得更多数据。

什么可能导致此错误?我可以关闭URLSpoofing检查吗?

有帮助吗?

解决方案

看起来domain.com的SSL证书已经提供给sub.domain.com。或者,更有可能的是,domain.com已被重命名为sub.domain.com而未更新SSL证书。

其他提示

cletus 对于可能的原因是正确的。

也有办法关闭欺骗检查。

您可以创建一个实现 HostnameVerifier 在比”通常“更多的情况下返回true。

您可以通过调用 setHostnameVerifier 在问题代码中的连接对象上。

这个答案的灵感来自于: http://www.java -samples.com/showtutorial.php?tutorialid=211

我发现此查询链接: http:/ /www.google.com/search?q=https+hostname+wrong+should+be

还有一点需要注意:在你这样做之前要三思而后行。您将在客户端和服务器组件之间的安全性中创建一个可利用的弱点。

我遇到了这个异常 - java.io.IOException:HTTPS主机名错误:应该是&lt; localhost&gt;

我的解决方案是我更改了自签名证书并制作 CN = localhost

OR

将证书域名 cn =&lt; domain-name&gt; 添加到主机文件中,该文件可能位于 c:/ windows / system32 / drivers / etc /...

以下代码解决了我的问题

static {
    //for localhost testing only
    javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
            new javax.net.ssl.HostnameVerifier() {

        @Override
        public boolean verify(String hostname,
                javax.net.ssl.SSLSession sslSession) {
            if (hostname.equals("your_domain")) {
                return true;
            }
            return false;
        }
    });
}

使用主机名(dns名称)作为别名。

例如:

keytool -import -alias <google.com> -file certificate_one.cer -keystore cacerts

默认情况下,Java会验证证书CN(公共名称)是否与URL中的主机名相同。如果证书中的 CN 主机名不同,则您的Web服务客户端将失败,并出现以下异常:java.io.IOException:HTTPS hostname wrong:should是证书中的主机名。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top