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 ());}}}