Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static SOAPMessage sendSoapRequest(String endpointUrl, SOAPMessage request) {
- try {
- final boolean isHttps = endpointUrl.toLowerCase().startsWith("https");
- HttpsURLConnection httpsConnection = null;
- // Open HTTPS connection
- if (isHttps) {
- // Create SSL context and trust all certificates
- SSLContext sslContext = SSLContext.getInstance("SSL");
- 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(endpointUrl);
- httpsConnection = (HttpsURLConnection) url.openConnection();
- // Trust all hosts
- httpsConnection.setHostnameVerifier(new TrustAllHosts());
- // Connect
- httpsConnection.connect();
- }
- // Send HTTP SOAP request and get response
- SOAPConnection soapConnection
- = SOAPConnectionFactory.newInstance().createConnection();
- SOAPMessage response = soapConnection.call(request, endpointUrl);
- // Close connection
- soapConnection.close();
- // Close HTTPS connection
- if (isHttps) {
- httpsConnection.disconnect();
- }
- Source src = response.getSOAPPart().getContent();
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer = tf.newTransformer();
- DOMResult result = new DOMResult();
- transformer.transform(src, result);
- System.out.println(convertXmlToString((Document) result.getNode()));
- return response;
- } catch (Exception ex) {
- // Do Something
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement