与所有受信任的主机一起发送带有https url的SOAP请求时出错

时间:2019-03-27 07:45:33

标签: java ssl soap

我正在尝试通过https URL发送肥皂请求,其中包含所有受信任的主机和证书,但是当我呼叫肥皂连接时,由于消息发送失败,它会显示错误。

我已经尝试在soap请求之前创建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

0 个答案:

没有答案