java.io.IOException:HTTPS主机名错误:应该是<domain name =“”> </domain>

时间:2014-03-20 13:47:30

标签: java connection

我正在尝试使用URLConnection使用下面的代码来获取网址,将错误视为

java.io.IOException: HTTPS hostname wrong:  should be <XXXX.XXXX.XXX>
    at sun.net.www.protocol.https.HttpsClient.checkURLSpoofing(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at protocol.URL_Certificate.main(URL_Certificate.java:55)

public class URL_Certificate {
    public static void main(String[] args) {
        String urlPlugin = "https://XXXX.XXXX.XXX/signin";

        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                @Override
                public void checkClientTrusted(final X509Certificate[] chain,
                        final String authType) {
                }

                @Override
                public void checkServerTrusted(final X509Certificate[] chain,
                        final String authType) {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            } };

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts,
                    new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext
                    .getSocketFactory();

            InputStream input;
            BufferedReader br;
            String line = "";

            // All set up, we can get a resource through https now:
            final URLConnection urlCon1 = new URL(urlPlugin).openConnection();
            // Tell the url connection object to use our socket factory which
            // bypasses security checks
            ((HttpsURLConnection) urlCon1)
                    .setSSLSocketFactory(sslSocketFactory);

            input = urlCon1.getInputStream();
            br = new BufferedReader(new InputStreamReader(input));
            line = "";
            while ((line = br.readLine()) != null) {
                // TODO: Need to Remove
                System.out.println(line);

            }
            br.close();
            input.close();

        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
}

我能够在浏览器中点击url获得响应,但无法通过代码.... 在上面的代码中,我也跳过了证书验证。

2 个答案:

答案 0 :(得分:0)

可能已经为子域设置了SSL证书,反之亦然?

这是你在这里写的正确网址:

String urlPlugin = "https://XXXX.XXXX.XXX/signin";

答案 1 :(得分:-3)

您可能还需要覆盖主机名验证程序:

sslSocketFactory.setHostnameVerifier(new HostnameVerifier() {
    public boolean verify(String hostname, SSLSession session) {
      return true;
    }
};

即使你已经做好了一切,java 1.7中也存在可以生成此错误的错误。解决方法是使用以下命令禁用SNI扩展:

"-Djsse.enableSNIExtension=false"