A more structural way to construct and to post a SOAP message to a web service can be accomplished by the use of SAAJ.
By using SAAJ your can dynamically construct your SOAP request. So it is possible, at runtime, to detect for example the operation style of the web service (by accessing the WSDL) and modify the namespace you will use in function of this type: document/literal -> body namespace (typically http://something/types or uri:something/types) or rpc/encoded -> type namespace (same but /wsdl).
The following example construct dynamically the SOAP message before to post it to the web service. The program simply echo the SOAP response returned by the service. You can parse the response using DOM or SAX to extract the values you need.
Be on a hurry? or just for debugging, you can send a text file containing the SOAP message with SAAJ. See the next section.
No more dynamic construction of the SOAP message this time, let's use a simple text editor and type the soap message we want to send.
Now the code will be shorter and you can easily use it for testing purpose.
Example 1-14. Post a SOAP message in a text file, to a web service using SAAJ
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import java.io.FileInputStream;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
public class Client {
public static void main(String[] args) {
try {
// Create the connection
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();
// Create message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
// Object for message parts
SOAPPart sp = msg.getSOAPPart();
StreamSource prepMsg = new StreamSource(
new FileInputStream("path/prepared.msg"));
sp.setContent(prepMsg);
// Save message
msg.saveChanges();
// View input
System.out.println("\n Soap request:\n");
msg.writeTo(System.out);
System.out.println();
// Send
String urlval = "http://www.pascalbotte.be/rcx-ws/rcx";
SOAPMessage rp = conn.call(msg, urlval);
// View the output
System.out.println("\nXML response\n");
// Create transformer
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
// Get reply content
Source sc = rp.getSOAPPart().getContent();
// Set output transformation
StreamResult result = new StreamResult(System.out);
tf.transform(sc, result);
System.out.println();
// Close connection
conn.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Using this method, you can send a soap message using SAAJ with only 10 lines of code!
![]() | ![]() | ![]() |
| Java HTTP post for XML SOAP message. | ![]() My home page Index RSS | Consume a .NET web service with Java |