SOAP 学习笔记

来源:互联网 发布:陈柏良知乎 编辑:程序博客网 时间:2024/05/23 20:13

SOAP:

Simple:
   用XML来呈现数据,并且通过HTTP来传输它是Simple的!
  
   SOAP请求的每个方面都试图成为一个自我描述并且基于大量验证集和构建良好的协议。
  
  
基于消息的文档交换和RPC:
SOAP在通过HTTP进行同步远程过程调用中有它自己的roots,

同步RPC,异步的基于消息的文档交换模型。

文档交换模型是两个endpoint之间数据交换的一种默认方法。

一个RPC Call实际上就是一个例子: 将多个单程异步报文合并成一个请求-响应
 
SOAP Message:
1)formatting conventions for encapsulating data:
   The envelope defines a convention for describing the contents of a message, which in turn has implications on how it gets processed
2)Encoding rules:                 provide a convention for mapping various application datatypes into an XML tag-based representation
3)a transport or protocol binding:provides a generic mechanism for sending a SOAP envelope via a lower-level protocol such as HTTP
4)RPC mechanism:                  provides a way to represent remote procedure calls and their return values


到底什么样的XML能成为SOAP ?


<?xml version="1.0" encoding="UTF-8"?>
<PurchaseOrder xmlns="urn:oreilly-jaws-samples">
    <shipTo country="US">
        <name>Joe Smith</name>
        <street>14 Oak Park</street>
        <city>Bedford</city>
        <state>MA</state>
        <zip>01730</zip>
    </shipTo>
    <items>
        <item partNum="872-AA">
            <productName>Candy Canes</productName>
            <quantity>444</quantity>
            <price>1.68</price>
            <comment>I want candy!</comment>
        </item>
    </items>
</PurchaseOrder>


还要哪些东西才能使上面这个普普通通的XML文档成为一个响当当的SOAP?
1  The wrapping of the XML inside of a SOAP body
2  The wrapping of the SOAP body within a SOAP envelope
3  The optional inclusion of a SOAP header block
4  Namespace declarations
5  Encoding style directives for the serialization of data
6  The binding of the whole thing to a protocol 


请看下面这个:
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="
http://schemas.xmlsoap.org/soap/envelope/
    xmlns:xsi="
http://www.w3.org/1999/XMLSchema-instance
    xmlns:xsd="
http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Header>
    ...
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<PurchaseOrder xmlns="urn:oreilly-jaws-samples">
        <shipTo country="US">
                <name>Joe Smith</name>
                <street>14 Oak Park</street>
                <city>Bedford</city>
                <state>MA</state>
                <zip>01730</zip>
        </shipTo>
        <items>
                <item partNum="872-AA">
                        <productName>Candy Canes</productName>
                        <quantity>444</quantity>
                        <price>1.68</price>
                        <comment>I want candy!</comment>
                </item>
        </items>
</PurchaseOrder>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


分析SOAP 声明部分:


<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="
http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="
http://www.w3.org/1999/XMLSchema-instance
    xmlns:xsd="
http://www.w3.org/1999/XMLSchema">
...
</SOAP-ENV:Envelope>

这三个属性是必须的!!!!
第一个属性:

  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

Actually, :Envelope and xmlns: are, but the use of the string SOAP-ENV is
completely arbitrary(任意的). What's really important is its relationship to the :Envelope and xmlns:
keywords.

其实这样也是可以地:
<abbr:Envelope 
    xmlns:abbr="
http://schemas.xmlsoap.org/soap/envelope/
    xmlns:xsi="
http://www.w3.org/1999/XMLSchema-instance
    xmlns:xsd="
http://www.w3.org/1999/XMLSchema">
...
</abbr:Envelope>

第二个属性:
xmlns:xsi=http://www.w3.org/1999/XMLSchema-instance
The prefix, xsi, must be prepended to all elements and attributes defined in this namespace
功能:declares the XML schema instance namespace
例如:xsi:type

第三个属性:
xmlns:xsd=http://www.w3.org/1999/XMLSchema
作用:defines the XMLSchema namespace

 

分析: The SOAP Header

a place to put directives to the SOAP processor that receives the message.
The <Body> is reserved purely for the method call and its parameters, and the <Header> is used for
things targeted at the underlying infrastructure, such as a transaction ID


<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="
http://schemas.xmlsoap.org/soap/envelope/
    xmlns:xsi="
http://www.w3.org/1999/XMLSchema-instance
    xmlns:xsd="
http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Header>
   <jaws:MessageHeader xmlns:jaws="urn:oreilly-jaws-samples">
        <From>Me</From>
        <To>You</To>
        <MessageId>9999</MessageId>
        ...
    </jaws:MessageHeader>
34
</SOAP-ENV:Header>
<SOAP-ENV:Body>
...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


分析:The SOAP Protocol Binding

SOAPAction = "urn:soaphttpclient-action-uri"
Host = localhost
Content-Type = text/xml; charset=utf-8
Content-Length = 701
 
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="
http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="
http://www.w3.org/1999/XMLSchema-instance
    xmlns:xsd="
http://www.w3.org/1999/XMLSchema">
...
</SOAP-ENV:Envelope>