用Soap消息调用Web Services

本文介绍如何利用Java API for XML Messaging (JAXM)简化SOAP消息的创建与发送流程。通过五个步骤——创建连接、创建消息、填充消息内容、发送消息及接收响应,演示了使用JAXM进行SOAP通信的具体实现。

如何使用用于 XML 消息传递的 Java APIJava API for XML Messaging (JAXM))简化创建和发送 SOAP 消息的过程。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Web 服务的基础在于以标准格式发送和接收消息以便使所有系统都能理解。通常,那种格式是简单对象访问协议(Simple Object Access Protocol (SOAP))。SOAP 消息可以手工生成和发送,但是用于 XML 消息传递的 Java APIJAXM)使许多必需步骤(如创建连接或创建并发送实际消息)自动化。

这个过程包含五个步骤:

创建 SOAP 连接

创建 SOAP 消息

填充消息

发送消息

检索应答

一个基本的 SOAP 消息由包含两个主要部分(报头和主体)的封套组成。应用程序决定如何使用这些部分,但整个消息必须遵循特定的 XML 结构(soap.msg文件
):

None.gif<?xmlversion="1.0"encoding="UTF-8"?>
None.gif
<soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
None.gif
<soap:Body>
None.gif
<ns1:SayHelloxmlns:ns1="http://boomga.com">
None.gif
</ns1:SayHello>
None.gif
</soap:Body>
None.gif
</soap:Envelope>
None.gif

请注意这个消息的结构。Envelope 包含Body 元素,而二者全都是 http://schemas.xmlsoap.org/soap/envelope/ 名称空间的一部分。整个消息将通过一个 SOAP 连接发送到一个 Web 服务中。

None.gifpublicstaticvoiddoSoapPost()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Firstcreatetheconnection
InBlock.gif
SOAPConnectionFactorysoapConnFactory=
InBlock.gifSOAPConnectionFactory.newInstance();
InBlock.gifSOAPConnectionconnection
=
InBlock.gifsoapConnFactory.createConnection();
InBlock.gif
InBlock.gif
//Next,createtheactualmessage
InBlock.gif
MessageFactorymessageFactory=MessageFactory.newInstance();
InBlock.gifSOAPMessagemessage
=messageFactory.createMessage();
InBlock.gif
InBlock.gif
//Createobjectsforthemessageparts
InBlock.gif
SOAPPartsoapPart=message.getSOAPPart();
InBlock.gifSOAPEnvelopeenvelope
=soapPart.getEnvelope();
InBlock.gifSOAPBodybody
=envelope.getBody();
InBlock.gif
InBlock.gif
//PopulatetheMessage
InBlock.gif
StreamSourcepreppedMsgSrc=newStreamSource(
InBlock.gif
newFileInputStream("E://soap.msg"));
InBlock.gifsoapPart.setContent(preppedMsgSrc);
InBlock.gif
//Savethemessage
InBlock.gif
message.saveChanges();
InBlock.gif
//Checktheinput
InBlock.gif
System.out.println("/nREQUEST:/n");
InBlock.gifmessage.writeTo(System.
out);
InBlock.gifSystem.
out.println();
InBlock.gif
//Sendthemessageandgetareply
InBlock.gif
InBlock.gif
//Setthedestination
InBlock.gif
Stringdestination=
InBlock.gif
"http://localhost:8000/HelloWorld/services/HelloWorldService";
InBlock.gif
//Sendthemessage
InBlock.gif
SOAPMessagereply=connection.call(message,destination);
InBlock.gif
InBlock.gif
//Checktheoutput
InBlock.gif
System.out.println("/nRESPONSE:/n");
InBlock.gif
//Createthetransformer
InBlock.gif
TransformerFactorytransformerFactory=
InBlock.gifTransformerFactory.newInstance();
InBlock.gifTransformertransformer
=
InBlock.giftransformerFactory.newTransformer();
InBlock.gif
//Extractthecontentofthereply
InBlock.gif
SourcesourceContent=reply.getSOAPPart().getContent();
InBlock.gif
//Settheoutputforthetransformation
InBlock.gif
StreamResultresult=newStreamResult(System.out);
InBlock.giftransformer.transform(sourceContent,result);
InBlock.gifSystem.
out.println();
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}

ExpandedBlockEnd.gif}

None.gif

运行结果:

None.gifREQUEST:
None.gif
None.gif
<?xmlversion="1.0"encoding="UTF-8"?>
None.gif
<soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
None.gif
<soap:Body>
None.gif
<ns1:SayHelloxmlns:ns1="http://boomga.com">
None.gif
</ns1:SayHello>
None.gif
</soap:Body>
None.gif
</soap:Envelope>
None.gif
None.gif
None.gif
None.gifRESPONSE:
None.gif
None.gif
<?xmlversion="1.0"encoding="UTF-8"?><soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><ns1:SayHelloResponsexmlns:ns1="http://boomga.com"><ns1:out>dyk,Hell0</ns1:out></ns1:SayHelloResponse></soap:Body></soap:Envelope>
None.gif
None.gif
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值