Pages

Thursday, December 8, 2011

Create soap client

// Web Service  
String url = "http://red.cse.yorku.ca:4413/axis/YYZ.jws";

// 1. Create a SOAP message
SOAPMessage msg = MessageFactory.newInstance().createMessage();
// 2. get header
MimeHeaders header =  msg.getMimeHeaders();
header.addHeader("SOAPAction", "");

// 3. get soap part
SOAPPart soap = msg.getSOAPPart();
// 4. get soap envelop
SOAPEnvelope envelop = soap.getEnvelope();
// 5. get soap body
SOAPBody body = envelop.getBody();
// 6. add body element
body.addChildElement("getName").addChildElement("itemNumber").addTextNode("2002H123");

// 7. open soap connection
SOAPConnection sc = SOAPConnectionFactory.newInstance().createConnection();
// 8. send soap request
SOAPMessage soapResponse = sc.call(msg, new URL(url));
// 9. close soap connection
sc.close();

System.out.println("SOAP Request ");
msg.writeTo(System.out); 
System.out.println("\n\nSOAP Response");
soapResponse.writeTo(System.out);

// 10. retrieve the response node
Node node = soapResponse.getSOAPPart().getEnvelope().getBody().getElementsByTagName("getNameReturn").item(0);

// 11. print out the Node text
System.out.println("\n\n"+node.getTextContent());

No comments:

Post a Comment