Skip to content

Instantly share code, notes, and snippets.

@laszlocsontos
Last active October 5, 2016 16:18
Show Gist options
  • Save laszlocsontos/b1ad208a21e8f58537d1430af479e449 to your computer and use it in GitHub Desktop.
Save laszlocsontos/b1ad208a21e8f58537d1430af479e449 to your computer and use it in GitHub Desktop.
Pseudo code for calling a WSDL-based web service without having to generate client stubs
import javax.xml.namespace.*;
import javax.xml.soap.*;
import org.w3c.dom.*;
// Create the message
MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage requestMessage = factory.createMessage();
// Add a header
SOAPHeader header = requestMessage.getSOAPHeader();
QName headerName = new QName(...);
SOAPHeaderElement headerElement = header.addHeaderElement(headerName);
...
// Add a body
QName bodyName = new QName(...);
SOAPBody body = requestMessage.getSOAPBody();
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
...
// Send the message
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
URL endpoint = new URL(...);
// Get the response
SOAPMessage responseMessage = connection.call(requestMessage, endpoint);
SOAPBody responseBody = responseMessage.getSOAPBody();
// Extracts a DOM Document and sets the first child of this SOAPBody as it's document element
Document responseDocument = responseBody.extractContentAsDocument();
...
// Finally
connection.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment