淘先锋技术网

首页 1 2 3 4 5 6 7

在《Soap消息调用Web Services(续)》这篇文章中介绍了如何在客户端发送Soap请求去调用服务器端的Web Service并输出服务器返回的结果,但还存在两个弱点,本文的目的就是对其进行改进,使得构造Soap请求发送到服务器端的流程完整。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

上文的弱点有二:1Soap请求是一个XML文件,而非灵活构造出来的。2)服务器端返回的结果仅仅是输出到控制台,而没有进行解析。

待构造的Soap请求:

None.gif < SOAP - ENV:Envelopexmlns:SOAP - ENV = " http://schemas.xmlsoap.org/soap/envelope/ " >
None.gif
< SOAP - ENV:Header / >
None.gif
< SOAP - ENV:Body >
None.gif
< ns1:getFriendsListxmlns:ns1 = " http://pojo.test.com " >
None.gif
< in0type = " int " > 1 < / in0>
None.gif
< in1type = " int " > 0 < / in1>
None.gif
< / ns1:getFriendsList>
None.gif
< / SOAP-ENV:Body>
None.gif
< / SOAP-ENV:Envelope>
None.gif

None.gif import java.io.ByteArrayInputStream;
None.gif
import java.io.ByteArrayOutputStream;
None.gif
import java.io.FileInputStream;
None.gif
import java.io.IOException;
None.gif
None.gif
import javax.xml.parsers.DocumentBuilder;
None.gif
import javax.xml.parsers.DocumentBuilderFactory;
None.gif
import javax.xml.parsers.ParserConfigurationException;
None.gif
import javax.xml.soap.MessageFactory;
None.gif
import javax.xml.soap.Name;
None.gif
import javax.xml.soap.SOAPBody;
None.gif
import javax.xml.soap.SOAPConnection;
None.gif
import javax.xml.soap.SOAPConnectionFactory;
None.gif
import javax.xml.soap.SOAPElement;
None.gif
import javax.xml.soap.SOAPEnvelope;
None.gif
import javax.xml.soap.SOAPMessage;
None.gif
import javax.xml.soap.SOAPPart;
None.gif
import javax.xml.transform.Source;
None.gif
import javax.xml.transform.Transformer;
None.gif
import javax.xml.transform.TransformerFactory;
None.gif
import javax.xml.transform.stream.StreamResult;
None.gif
import javax.xml.transform.stream.StreamSource;
None.gif
None.gif
import org.w3c.dom.Document;
None.gif
import org.w3c.dom.Element;
None.gif
import org.w3c.dom.NodeList;
None.gif
import org.xml.sax.SAXException;
None.gif
None.gif
public class SoapParser
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif
publicstaticvoidmain(String[]args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifdoSoapPost();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
publicstaticvoiddoSoapPost()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Firstcreatetheconnection
InBlock.gif
SOAPConnectionFactorysoapConnFactory=SOAPConnectionFactory.newInstance();
InBlock.gifSOAPConnectionconnection
=soapConnFactory.createConnection();//创建连接
InBlock.gif
InBlock.gif
//Next,createtheactualmessage
InBlock.gif
MessageFactorymessageFactory=MessageFactory.newInstance();
InBlock.gifSOAPMessagemessage
=messageFactory.createMessage();//创建soap请求
InBlock.gif
InBlock.gif
//Createobjectsforthemessageparts
InBlock.gif
SOAPPartsoapPart=message.getSOAPPart();
InBlock.gifSOAPEnvelopeenvelope
=soapPart.getEnvelope();
InBlock.gifSOAPBodybody
=envelope.getBody();
InBlock.gif
InBlock.gif
////Populatethebody
InBlock.gif
////Createthemainelementandnamespace
InBlock.gif
SOAPElementbodyElement=body.addChildElement(envelope.createName("getFriendsList","ns1","http://pojo.test.com"));
InBlock.gif
//Addcontent
InBlock.gif
SOAPElementfirstElemnt=bodyElement.addChildElement("in0");
InBlock.gifNamefirstName
=envelope.createName("type");
InBlock.giffirstElemnt.addAttribute(firstName,
"int");
InBlock.giffirstElemnt.addTextNode(
"1");
InBlock.gif
InBlock.gifSOAPElementsecondElemnt
=bodyElement.addChildElement("in1");
InBlock.gifNamesecondName
=envelope.createName("type");
InBlock.gifsecondElemnt.addAttribute(secondName,
"int");
InBlock.gifsecondElemnt.addTextNode(
"0");
InBlock.gif
InBlock.gif
//Savethemessage
InBlock.gif
message.saveChanges();
InBlock.gif
//Checktheinput
InBlock.gif
message.writeTo(System.out);
InBlock.gifSystem.out.println();
InBlock.gif
//Sendthemessageandgetareply
InBlock.gif
InBlock.gif
//Setthedestination
InBlock.gif
Stringdestination=
InBlock.gif
"http://192.168.1.10:8080/myTest/services/MyService";
InBlock.gif
//Sendthemessage
InBlock.gif
SOAPMessagereply=connection.call(message,destination);
InBlock.gif
InBlock.gif
if(reply!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSOAPPartreplySP
=reply.getSOAPPart();
InBlock.gifSOAPEnvelopereplySE
=replySP.getEnvelope();
InBlock.gifSOAPBodyreplySB
=replySE.getBody();
InBlock.gif
InBlock.gifSourcesource
=reply.getSOAPPart().getContent();
InBlock.gifTransformertransformer
=TransformerFactory.newInstance().newTransformer();
InBlock.gifByteArrayOutputStreammyOutStr
=newByteArrayOutputStream();
InBlock.gifStreamResultres
=newStreamResult();
InBlock.gifres.setOutputStream(myOutStr);
InBlock.giftransformer.transform(source,res);
InBlock.gifStringtemp
=myOutStr.toString("UTF-8");
InBlock.gif
InBlock.gifSystem.out.println(temp);
InBlock.gif
byte[]bytes=temp.getBytes("UTF-8");
InBlock.gifByteArrayInputStreamin
=newByteArrayInputStream(bytes);
InBlock.gif
InBlock.gifDocumentBuilderFactorydbf
=DocumentBuilderFactory.newInstance();
InBlock.gifDocumentBuilderdb
=null;
InBlock.gifDocumentdoc
=null;
InBlock.gifdb
=dbf.newDocumentBuilder();
InBlock.gifdoc
=db.parse(in);
InBlock.gifElementdocEle
=doc.getDocumentElement();
InBlock.gifNodeListnl
=docEle.getElementsByTagName("ns2:FriendsList");
InBlock.gif
if(nl!=null&&nl.getLength()>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(inti=0;i<nl.getLength();i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//gettheemployeeelement
InBlock.gif
Elementel=(Element)nl.item(i);
InBlock.gifStringname
=getTextValue(el,"name");
InBlock.gif
intid=getIntValue(el,"userId");
InBlock.gifSystem.out.println(
"name:"+name+"id:"+id);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
//Closetheconnection
InBlock.gif
connection.close();
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(Exceptione)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.out.println(e.getMessage());
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif
privatestaticStringgetTextValue(Elementele,StringtagName)dot.gif{
InBlock.gifStringtextVal
=null;
InBlock.gifNodeListnl
=ele.getElementsByTagName(tagName);
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(nl!=null&&nl.getLength()>0)dot.gif{
InBlock.gifElementel
=(Element)nl.item(0);
InBlock.giftextVal
=el.getFirstChild().getNodeValue();
ExpandedSubBlockEnd.gif}

InBlock.gif
returntextVal;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif
privatestaticintgetIntValue(Elementele,StringtagName)dot.gif{
InBlock.gif
//inproductionapplicationyouwouldcatchtheexception
InBlock.gif
returnInteger.parseInt(getTextValue(ele,tagName));
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
privatestaticvoidparseXmlFile(StringfileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//getthefactory
InBlock.gif
DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();
ExpandedSubBlockStart.gifContractedSubBlock.gif
trydot.gif{
InBlock.gif
//Usingfactorygetaninstanceofdocumentbuilder
InBlock.gif
DocumentBuilderdb=dbf.newDocumentBuilder();
InBlock.gif
//parseusingbuildertogetDOMrepresentationoftheXMLfile
InBlock.gif
Documentdom=db.parse(fileName);
ExpandedSubBlockStart.gifContractedSubBlock.gif}
catch(ParserConfigurationExceptionpce)dot.gif{
InBlock.gifpce.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif}
catch(SAXExceptionse)dot.gif{
InBlock.gifse.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif}
catch(IOExceptionioe)dot.gif{
InBlock.gifioe.printStackTrace();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif