На моей машине (в основном методе) я могу использовать веб-службу https, используя этот код:
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();
Обратите внимание, что в коде я обхожу проверку имени хоста, согласно ответам на этот пост: Java SSLException: имя хоста в сертификате не совпадает
Но если я попытаюсь использовать ту же самую веб-службу https, используя тот же код, на той же машине (мой локальный компьютер), но из веб-приложения, развернутого на Tomcat 8, я получаю сообщение об ошибке:
Caused by: javax.net.ssl.SSLHandshakeException:
java.security.cert.CertificateException: No name matching localhost found
Я искал ответы и решения этой проблемы, но ничего не нашел. Я не знаю. У вас есть идеи, почему это происходит?