background image

Request.java Class Code

<< Code Examples | MyUddiPing.java >>
<< Code Examples | MyUddiPing.java >>
152
SOAP
WITH
A
TTACHMENTS
API
FOR
J
AVA
Request.java
The class
Request.java
puts together the code fragments used in the section
Tutorial (page 123) and adds what is needed to make it a complete example of a
client sending a request-response message. In addition to putting all the code
together, it adds
import
statements, a
main
method, and a
try
/
catch
block with
exception handling.
import javax.xml.soap.*;
import javax.xml.namespace.QName;
import java.util.Iterator;
import java.net.URL;
public class Request {
public static void main(String[] args){
try {
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnectionFactory.createConnection();
MessageFactory factory =
MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
header.detachNode();
QName bodyName = new QName("http://wombat.ztrade.com",
"GetLastTradePrice", "m");
SOAPBodyElement bodyElement =
body.addBodyElement(bodyName);
QName name = new QName("symbol");
SOAPElement symbol =
bodyElement.addChildElement(name);
symbol.addTextNode("SUNW");
URL endpoint = new URL
("http://wombat.ztrade.com/quotes");
SOAPMessage response =
connection.call(message, endpoint);
connection.close();
SOAPBody soapBody = response.getSOAPBody();