On my machine (on a main method) I am able to consume an https web service using this code:
URL url;
HttpsURLConnection connection;
HttpsURLConnection.setDefaultHostnameVerifier(
new javax.net.ssl.HostnameVerifier(){
public boolean verify(String hostname,
javax.net.ssl.SSLSession sslSession) {
return true;
}
});
//prepare the request
byte[] postData = xml.getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
url = new URL(ENDPOINT_URL);//https endpoint
connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("Content-Length", Integer.toString(postDataLength));
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(postData);
//make the request and get response xml string
connection.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
Notice that on the code I bypass hostname verification, according to the answers to this post: Java SSLException: hostname in certificate didn't match
But if I try to consume the exact same https web service, using the exact same code, on the the exact same machine (my local machine), but from a webapp deployed on Tomcat 8, I get the error:
Caused by: javax.net.ssl.SSLHandshakeException:
java.security.cert.CertificateException: No name matching localhost found
I have searched for anwsers and solutions to this problem but found none. I am clueless. Do you have any idea why is this happening?