【问题标题】:How to access a HTTPS url from a java application如何从 Java 应用程序访问 HTTPS url
【发布时间】:2013-10-26 11:56:09
【问题描述】:

我知道这是一个非常基本的问题,但是我如何设法找不到解决此问题的方法。我有一个具有 main 方法的 java 类。在该方法中,我尝试访问 https url,如下所示:

package helloworld;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class ConnectHttps
{
public static void main(String[] argsd)
{
    System.out.println("***************Https testing started **************");
    try
    {
        URL u = new URL("https://localhost:8443/myapp/test");
        HttpsURLConnection http = (HttpsURLConnection) u.openConnection();
        http.setAllowUserInteraction(true);
        http.setRequestMethod("GET");
        http.connect();

        InputStream is = http.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null)
        {
            stringBuilder.append(line
                + "\n");
        }
        System.out.println(stringBuilder.toString());
        System.out.println("***************Https testing completed **************");
    }
    catch (IOException e)
    {
        System.out.println("***************Https testing failed **************");
        e.printStackTrace();
    }
}

}

在执行这个程序时,我得到的输出是:

***************Https testing started **************
***************Https testing failed **************
javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching localhost found
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:515)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
at helloworld.ConnectHttps.main(ConnectHttps.java:59)

我想我在这里犯了一个非常基本的错误。

我使用的是 JDK 1.7.0_25。

【问题讨论】:

  • @axiopisty 为什么?如果 JSSE 没有“正确配置”,他怎么可能编译过这个程序?还是执行了?或者得到一个包含 sun.security.ssl.SSLSocketImpl 的堆栈跟踪?

标签: java ssl https jsse httpsurlconnection


【解决方案1】:

java.net.ConnectException: Connection timed out: connect

这实际上与 SSL/TLS 无关。相反,您的客户端根本无法连接到服务器(至少不是在合理的时间内)。

很可能有防火墙阻止您进行此类连接。

您可能需要通过代理,在这种情况下,HttpsURLConnection 应考虑设置https.proxyHosthttps.proxyPort 系统属性。

【讨论】:

  • 感谢Bruno 的回复。我已经编辑了我的问题以指向一个本地 URL。我之前提供了一个公共 URL,以便它可以是一个完整的 sscce。我得到了例外:javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching localhost found。我做了我的研究以到达this,但这所做的只是禁用 SSL。
  • 基本上我需要的是能够在我的客户端上配置安全证书,在这种情况下它不是浏览器。
  • 您的证书对localhost 无效。尝试在 URL 中使用 FQDN,以匹配证书中的内容。
  • 虽然听起来很天真,但我使用 keytool 添加证书,但不确定我是否朝着正确的方向前进。
  • 另外,对于开发以外的环境,这可能会再次导致类似的异常,因为这些环境的证书会有所不同。
猜你喜欢
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 2015-03-18
  • 2016-01-02
相关资源
最近更新 更多