Access soap using SAAJ

Source: Internet
Author: User
1. sopa1.1
Package SAAJ. my; import javax. XML. soap. messagefactory; import javax. XML. soap. mimeheaders; import javax. XML. soap. soapbody; import javax. XML. soap. soapconnection; import javax. XML. soap. soapconnectionfactory; import javax. XML. soap. soapconstants; import javax. XML. soap. soapelement; import javax. XML. soap. soapenvelope; import javax. XML. soap. soapheader; import javax. XML. soap. soapmessage; import javax. XML. soap. soappart; import javax. XML. transform. source; import javax. XML. transform. transformer; import javax. XML. transform. transformerfactory; import javax. XML. transform. stream. streamresult; public class testsaajsoap1_1 {public static void main (string ARGs []) {try {// first create the connectionsoapconnectionfactory soapconnfactory = soapconnectionfactory. newinstance (); soapconnection connection = soapconnfactory. createconnection (); // next, create the actual messagemessagefactory messagefactory = messagefactory. newinstance (soapconstants. soap_zookeeper Protocol); soapmessage message = messagefactory. createmessage (); // header information of the HTTP request mimeheaders headers = message. getmimeheaders (); headers. addheader ("soapaction", "http://WebXml.com.cn/getWeatherbyCityName"); // create objects for the message partssoappart soappart = message. getsoappart (); soapenvelope envelope = soappart. getenvelope (); // remove the soapheader header = envelope. getheader (); header. detachnode (); soapbody body = envelope. getbody (); // populate the body // create the main element and namespacesoapelement bodyelement = body. addchildelement (envelope. createname ("getweatherbycityname", "", "http://WebXml.com.cn/"); // Add contentbodyelement. addchildelement ("thecityname "). addtextnode ("Guangzhou"); // Save the messagemessage. savechanges (); // check the inputsystem. out. println ("\ nrequest: \ n"); message. writeto (system. out); system. out. println (); // send the message and get a reply // set the destinationstring Destination = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; // send the messagesoapmessage reply = connection. call (message, destination); // check the output system. out. println ("\ nresponse: \ n"); // create the transformer transformerfactory = transformerfactory. newinstance (); transformer = transformerfactory. newtransformer (); // extract the content of the reply source sourcecontent = reply. getsoappart (). getcontent (); // set the output for the transformation streamresult result = new streamresult (system. out); transformer. transform (sourcecontent, result); system. out. println (); connection. close ();} catch (exception e) {system. out. println (E. getmessage ());}}}

2. soap1.2

Package SAAJ. my; import javax. XML. soap. messagefactory; import javax. XML. soap. soapbody; import javax. XML. soap. soapconnection; import javax. XML. soap. soapconnectionfactory; import javax. XML. soap. soapconstants; import javax. XML. soap. soapelement; import javax. XML. soap. soapenvelope; import javax. XML. soap. soapheader; import javax. XML. soap. soapmessage; import javax. XML. soap. soappart; import javax. XML. transform. source; import javax. XML. transform. transformer; import javax. XML. transform. transformerfactory; import javax. XML. transform. stream. streamresult; public class testsaajsoap1_2 {public static void main (string ARGs []) {try {// first create the connectionsoapconnectionfactory soapconnfactory = soapconnectionfactory. newinstance (); soapconnection connection = soapconnfactory. createconnection (); // next, create the actual messagemessagefactory messagefactory = messagefactory. newinstance (soapconstants. soap_1_2_protocol); soapmessage message = messagefactory. createmessage (); // create objects for the message partssoappart soappart = message. getsoappart (); soapenvelope envelope = soappart. getenvelope (); // remove the soapheader header = envelope. getheader (); header. detachnode (); soapbody body = envelope. getbody (); // populate the body // create the main element and namespacesoapelement bodyelement = body. addchildelement (envelope. createname ("getweatherbycityname", "", "http://WebXml.com.cn/"); // Add contentsoapelement soapelement = bodyelement. addchildelement ("thecityname"); soapelement. addtextnode ("Shanghai"); // Save the messagemessage. savechanges (); // check the inputsystem. out. println ("\ nrequest: \ n"); message. writeto (system. out); system. out. println (); // send the message and get a reply // set the destinationstring Destination = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; // send the messagesoapmessage reply = connection. call (message, destination); // check the output system. out. println ("\ nresponse: \ n"); // create the transformer transformerfactory = transformerfactory. newinstance (); transformer = transformerfactory. newtransformer (); // extract the content of the reply source sourcecontent = reply. getsoappart (). getcontent (); // set the output for the transformation streamresult result = new streamresult (system. out); transformer. transform (sourcecontent, result); system. out. println (); connection. close ();} catch (exception e) {system. out. println (E. getmessage ());}}}

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.