在WSDL中融入SOAP简例

    WSDL(网络服务描述语言 Web Services Description Language)是一门基于XML的语言,用于描述Web Services 以及如何对它们进行访问。

    WSDL文档描述元素:

            <portType>  执行的操作

            <message>   使用的消息

            <types>     使用的数据类型

            <binding>   使用的通信协议

      

     下面是一个 WSDL 文档的简化的片段:

<message name="getTermRequest">
<part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
<part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
<operation name="getTerm">
<input message="getTermRequest"/>
<output message="getTermResponse"/>
</operation>
</portType>

     在这个例子中,<portType> 元素把 "glossaryTerms" 定义为某个端口的名称,把 "getTerm" 定义为某个操作的名称。

操作 "getTerm" 拥有一个名为 "getTermRequest" 的输入消息,以及一个名为 "getTermResponse" 的输出消息。

     <message> 元素可定义每个消息的部件,以及相关联的数据类型。

对比传统的编程,glossaryTerms 是一个函数库,而 "getTerm" 是带有输入参数 "getTermRequest" 和返回参数 getTermResponse 的一个函数。

     <portType>元素是最重要的WSDL元素

可以描述一个 web service可被执行的操作,以及相关的消息,端口定义了指向 web service的连接点。可以把该元素比作传统变成语言中的一个函数库,每个操作比作一个函数

   

     绑定到 SOAP,一个 请求 - 响应 操作的例子:

<message name="getTermRequest">
<part name="term" type="xs:string" />
</message>

<message name="getTermResponse">
<part name="value" type="xs:string" />
</message>

<portType name="glossaryTerms">
<operation name="getTerm">
<input message="getTermRequest" />
<output message="getTermResponse" />
</operation>
</portType>

<binding type="glossaryTerms" name="b1">
<soap:binding style="document"
transport
="http://schemas.xmlsoap.org/soap/http" />
<operation>
<soap:operation
soapAction="http://example.com/getTerm" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>

 

  </binding>binding 元素有两个属性 - name 属性和 type 属性。

  name 属性定义 binding 的名称,而 type 属性指向用于 binding 的端口,在这个例子中是 "glossaryTerms" 端口。

 soap:binding 元素有两个属性 - style 属性和 transport 属性。

 style 属性可取值 "rpc" "document"。在这个例子中我们使用 documenttransport 属性定义了要使用的 SOAP 协议。在这个例子中我们使用 HTTP

 operation 元素定义了每个端口提供的操作符。

 对于每个操作,相应的 SOAP 行为都需要被定义。同时您必须如何对输入和输出进行编码。在这个例子中我们使用了 "literal"

 

 

    一个 SOAP 实例

        在下面的例子中,一个 GetStockPrice 请求被发送到了服务器。此请求有一个 StockName 参数,而在响应中则会返回一个 Price 参数。

    此功能的命名空间被定义在此地址中:http://www.example.org/stock

       SOAP 请求:

POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle
="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
</m:GetStockPrice>
</soap:Body>

</soap:Envelope>

 

    

    SOAP 响应:

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle
="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPriceResponse>
<m:Price>34.5</m:Price>
</m:GetStockPriceResponse>
</soap:Body>

</soap:Envelope>

 

 

 

posted @ 2010-08-19 16:53  gistone  Views(410)  Comments(0)    收藏  举报