when I request something using the http protocol, my code works and I get the expected response. But when I create a version to work with https, I get an SSL error. 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. I don't seem to get an error on that step, the error occurs on the line inputStream = connection.getInputStream();
. I don't know what else I need to add/set to set up the scure connection?
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)