Advertisement
Fhernd

envio-solicitud-soap.java

Dec 13th, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. public static SOAPMessage sendSoapRequest(String endpointUrl, SOAPMessage request) {
  2.         try {
  3.             final boolean isHttps = endpointUrl.toLowerCase().startsWith("https");
  4.             HttpsURLConnection httpsConnection = null;
  5.             // Open HTTPS connection
  6.             if (isHttps) {
  7.                 // Create SSL context and trust all certificates
  8.                 SSLContext sslContext = SSLContext.getInstance("SSL");
  9.                 TrustManager[] trustAll
  10.                         = new TrustManager[] {new TrustAllCertificates()};
  11.                 sslContext.init(null, trustAll, new java.security.SecureRandom());
  12.                 // Set trust all certificates context to HttpsURLConnection
  13.                 HttpsURLConnection
  14.                         .setDefaultSSLSocketFactory(sslContext.getSocketFactory());
  15.                
  16.                 // Open HTTPS connection
  17.                 URL url = new URL(endpointUrl);
  18.                 httpsConnection = (HttpsURLConnection) url.openConnection();
  19.                
  20.                 // Trust all hosts
  21.                 httpsConnection.setHostnameVerifier(new TrustAllHosts());
  22.                 // Connect
  23.                 httpsConnection.connect();
  24.             }
  25.            
  26.             // Send HTTP SOAP request and get response
  27.             SOAPConnection soapConnection
  28.                     = SOAPConnectionFactory.newInstance().createConnection();
  29.            
  30.             SOAPMessage response = soapConnection.call(request, endpointUrl);
  31.             // Close connection
  32.             soapConnection.close();
  33.             // Close HTTPS connection
  34.             if (isHttps) {
  35.                 httpsConnection.disconnect();
  36.             }
  37.            
  38.             Source src = response.getSOAPPart().getContent();  
  39.             TransformerFactory tf = TransformerFactory.newInstance();  
  40.             Transformer transformer = tf.newTransformer();  
  41.             DOMResult result = new DOMResult();  
  42.             transformer.transform(src, result);
  43.            
  44.             System.out.println(convertXmlToString((Document) result.getNode()));
  45.            
  46.             return response;
  47.         } catch (Exception ex) {
  48.             // Do Something
  49.         }
  50.         return null;
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement