Operate SOAP messages on java clients

Source: Internet
Author: User
Package org. example. myservice; import java. io. IOException; import java.net. malformedURLException; import java.net. URL; import javax. xml. namespace. QName; import javax. xml. soap. messageFactory; import javax. xml. soap. SOAPBody; import javax. xml. soap. SOAPElement; import javax. xml. soap. SOAPEnvelope; import javax. xml. soap. SOAPException; import javax. xml. soap. SOAPMessage; import javax. xml. soap. SOAPPart; import javax. xm L. ws. dispatch; import javax. xml. ws. service; import org. junit. test; import org. w3c. dom. node; import org. w3c. dom. nodeList; public class JunitTest {private String ns = "http://www.example.org/MyService/"; private String wsdlUrl = "http: // 10.10.9.208: 8088/MS? Wsdl "; @ Testpublic void testMessage () {// 1, create a message factory try {MessageFactory factory = MessageFactory. newInstance (); // 2. Create SOAPMessageSOAPMessage messge = factory based on the message factory. createMessage (); // 3, create SOAPPartSOAPPart part = messge. getSOAPPart (); // 4, get the envelope SOAPEnvelope envelop = part. getEnvelope (); // 5. Obtain the SOAPBody body = envelop in the message topic. getBody (); // 6, create information QName qname = new QName (wsdlUrl, "add", "ns"); SOAPElement ele = body. addBodyElement (qn Ame); ele. addChildElement ("number1 "). setValue ("1"); ele. addChildElement ("number2 "). setValue ("2"); messge. writeTo (System. out);} catch (SOAPException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}/// test to send the message to the server @ Testpublic void testSendMessge () {// 1, and create the Service try {URL url = new URL (wsdlUrl ); QName qname = new QName (ns, "MySe RviceImplService "); Service service Service = Service. create (url, qname); // 2, create DispicathcDispatch <SOAPMessage> dispatch = service. createDispatch (new QName (ns, "MyServiceImplPort"), SOAPMessage. class, Service. mode. MESSAGE); // 3, create the message SOAPMessage MESSAGE = MessageFactory. newInstance (). createMessage (); SOAPBody body = message. getSOAPPart (). getEnvelope (). getBody (); SOAPElement ele = body. addChildElement (new QName (ns, "add", "nn" ); Ele. addChildElement ("number1 "). setValue ("12"); ele. addChildElement ("number2 "). setValue ("13"); SOAPMessage response = dispatch. invoke (message); // response. writeTo (System. out); NodeList nodeList = response. getSOAPBody (). getElementsByTagName ("result"); Node node = nodeList. item (0); System. out. println (node. getTextContent (); // you can use the expected value in this way .} Catch (MalformedURLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (SOAPException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.