简体   繁体   English

Java请求中的HTTPS SSL GET失败

[英]HTTPS SSL GET in java request fails

when I request something using the http protocol, my code works and I get the expected response. 当我使用http协议请求某些内容时,我的代码有效,并且得到了预期的响应。 But when I create a version to work with https, I get an SSL error. 但是,当我创建一个可与https一起使用的版本时,出现了SSL错误。 I already followed the steps in this question , and I don't think I need the key/trustmanagers as described in this question . 我已经按照此问题中的步骤进行操作,并且我认为我不需要此问题中所述的密钥/信任管理器。 See the code below. 请参见下面的代码。 I set the key and trustmanager to null so java will take the default ones. 我将key和trustmanager设置为null,因此java将采用默认值。 I don't seem to get an error on that step, the error occurs on the line inputStream = connection.getInputStream(); 我似乎没有在该步骤上出错,该错误发生在inputStream = connection.getInputStream();行上inputStream = connection.getInputStream(); . I don't know what else I need to add/set to set up the scure connection? 我不知道我还需要添加/设置scure连接吗?

private String runSecureService(URL url)
{
    HttpsURLConnection connection = null;
    InputStream inputStream = null;
    SSLContext sslContext = null;

    try
    {
        sslContext = SSLContext.getInstance("SSL");
    }
    catch (NoSuchAlgorithmException e)
    {
        //this exception doesn't occur
    }

    try
    {
        sslContext.init(null, null, null);
    }
    catch (KeyManagementException e)
    {
        //this exception doesn't occur
    }

    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

    try
    {
        connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestProperty("Authorization", clientCredentials.getBase64Authentication());

    !-----> inputStream = connection.getInputStream(); <------!
        InputStreamReader responseReader = new InputStreamReader(inputStream);

        //Read, print and return the response.
        ...

        return response;
    }
    catch (IOException e)
    {
        //do stuff...
    }
    finally
    {
        //close connection and inputstream...
    }
}

here's the error: 这是错误:

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at sun.security.ssl.InputRecord.handleUnknownRecord(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(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)

You can get this message if you attempt to make an HTTPS request, but connect to the HTTP port on the remote server. 如果尝试发出HTTPS请求,但连接到远程服务器上的HTTP端口,则会收到此消息。

The chances are that you are either using the wrong port number for HTTPS in your request ... or the server is misconfigured and has an HTTP connection listener on what should be the HTTPS port. 可能是您在请求中为HTTPS使用了错误的端口号...或服务器配置错误,并且在应该是HTTPS端口的位置上具有HTTP连接侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM