Ошибка при отправке SOAP-запроса с https url всем доверенным хостом - PullRequest
0 голосов
/ 27 марта 2019

Я пытаюсь отправить запрос мыла через URL-адрес https со всеми доверенными хостами и сертификатами, но когда я вызываю соединение мыла, он выдает ошибку, поскольку отправка сообщения не удалась.

Я уже пытался создать HTTPUrlConnection перед запросом мыла, а также установить и настроить верификатор имени хоста, прежде чем доверять всем хостам

public static SOAPMessage makeSoapRequest(String xml, String endPoint)  {
        try {
            final boolean isHttps = endPoint.toLowerCase().startsWith("https");
            HttpsURLConnection httpsConnection = null;
            if (isHttps) {
                logger.info("Inside https class");
                String protocal="SSL";
                SSLContext sslContext = SSLContext.getInstance(protocal);
                logger.info("Protocal==>"+protocal);
                TrustManager[] trustAll
                        = new TrustManager[] {new TrustAllCertificates()};
                sslContext.init(null, trustAll, new java.security.SecureRandom());
                // Set trust all certificates context to HttpsURLConnection
                HttpsURLConnection
                        .setDefaultSSLSocketFactory(sslContext.getSocketFactory());
                // Open HTTPS connection
                URL url = new URL(endPoint);
                httpsConnection = (HttpsURLConnection) url.openConnection();
                // Trust all hosts
                httpsConnection.setHostnameVerifier(new TrustAllHosts());
                // Connect
                httpsConnection.connect();
            }

            logger.info("Starting soap connection");
            MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage message = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))));
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            try {
            logger.info("Before soap connection");
            SOAPMessage soapResponse = soapConnection.call(message, endPoint);
            logger.info("After soap connection");

            logger.info("Closing soap connection");
            soapConnection.close();
            if (isHttps) {
                logger.info("https connection close");
                httpsConnection.disconnect();
            }
                return soapResponse;
            } catch (Exception e) {
                logger.info("error in connection close");
                logger.info(e.getMessage());
                e.printStackTrace();
            }
        }catch (SOAPException | IOException
                | NoSuchAlgorithmException | KeyManagementException ex) {
            logger.info("Inside soap exception");
            ex.printStackTrace();
        }
        catch (Exception e){
            logger.info(e.getMessage());
        }
            return null;

    }

private static class TrustAllCertificates implements X509TrustManager {
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }

    private static class TrustAllHosts implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
         }
    }

Я получаю ошибку

2019-03-27 13:03:10,380 ERROR com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection [http-nio-4134-exec-2] SAAJ0009: Message send failed
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...