WSDL 基础

WSDL 指网络服务描述语言 (Web Services Description Language)。

WSDL 是一种使用 XML 编写的文档。这种文档可描述某个 Web service。它可规定服务的位置,以及此服务提供的操作(或方法)。

服务描述的相关组件
<wsdl:import>   引用另一个 文档,将其描述加到本文档中。
<wsdl:types>    定义消息交换所使用的 XML 类型和元素。
<wsdl:message>  定义一个实际的消息,包含 XML 类型或元素。
<wsdl:portType> 定义一个服务所实现的操作抽象集。
<wsdl:binding>  定义 <wsdl:portType> 的一个使用特定协议和格式的具体实现。
<wsdl:service>  定义一个服务整体,包括一个或多个包含 <wsdl:binding> 元素访问信息的 <wsdl:port> 元素。

服务描述的主要结构
<definitions>
<types>
   definition of types........
</types>
<message>
   definition of a message....
</message>
<portType>
   definition of a port.......
</portType>
<binding>
   definition of a binding....
</binding>
</definitions>

详细说明
1、WSDL 端口
<portType> 元素是最重要的 WSDL 元素。
它可描述一个 web service、可被执行的操作,以及相关的消息。
可以把 <portType> 元素比作传统编程语言中的一个函数库(或一个模块、或一个类)。
2、WSDL 消息
<message> 元素定义一个操作的数据元素。
每个消息均由一个或多个部件组成。可以把这些部件比作传统编程语言中一个函数调用的参数。
3、WSDL types
<types> 元素定义 web service 使用的数据类型。
为了最大程度的平台中立性,WSDL 使用 XML Schema 语法来定义数据类型。
4、WSDL Bindings
<binding> 元素为每个端口定义消息格式和协议细节。

WSDL文档示例
下例是一个提供股票报价的简单Web服务的 WSDL 定义。该服务支持名为 GetLastTradePrice 的单一操作,这个操作是通过在 HTTP 上运行 SOAP 1.1 协议来实现的。该请求接受一个类型为字符串的 tickerSymbol,并返回类型为浮点数的价格。
<?xml version="1.0"?>
<definitions name="StockQuote"
             targetNamespace="http://example.com/stockquote.wsdl"
             xmlns:tns="http://example.com/stockquote.wsdl"
             xmlns:xsd1="http://example.com/stockquote.xsd"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns="http://schemas.xmlsoap.org/wsdl/">
            

  <types>
    <schema targetNamespace="http://example.com/stockquote.xsd"
            xmlns="http://www.w3.org/1999/XMLSchema">
      <element name="TradePriceRequest">
        <complexType>
          <all>
            <element name="tickerSymbol" type="string"/>
          </all>
        </complexType>
      </element>
      <element name="TradePriceResult">
        <complexType>
          <all>
            <element name="price" type="float"/>
          </all>
        </complexType>
      </element>
    </schema>
  </types>
上面这部分是数据类型的定义,其中为定义了两个元素的结构:

TradePriceRequest(交易价格请求): 将该元素定义为包含一个字符串元素(tickerSymbol)的复合类型元素。
TradePriceResult(交易价格): 将该元素定义为一个包含一个浮点数元素(price)的复合类型元素。
<message name="GetLastTradePriceInput">
    <part name="body" element="xsd1:TradePriceRequest"/>
  </message>

  <message name="GetLastTradePriceOutput">
    <part name="body" element="xsd1:TradePriceResult"/>
  </message>
这部分是消息格式的抽象定义,其中定义了两个消息格式:

GetlastTradePriceInput(获取最后交易价格的请求消息格式): 由一个消息片断组成,该消息片断的名字是body,包含的具体元素类型是TradePriceRequest。(前面已经定义过了)
GetLastTradePriceOutput(获取最后交易价格的响应消息格式) : 由一个消息片断组成,该消息片断的名字是body,包含的具体元素类型是TradePriceResult。(前面已经定义过了)
<portType name="StockQuotePortType">
    <operation name="GetLastTradePrice">
      <input message="tns:GetLastTradePriceInput"/>
      <output message="tns:GetLastTradePriceOutput"/>
    </operation>
  </portType>
这部分定义了服务访问点的调用模式的类型,表明StockQuoteService的某个入口类型是请求/响应模式,请求消息是GetlastTradePriceInput,而响应消息是GetLastTradePriceOutput。

<binding name="StockQuoteSoapBinding" type="tns:StockQuotePortType">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      <operation name="GetLastTradePrice">
        <soap:operation soapAction="http://example.com/GetLastTradePrice"/>
          <input>
            <soap:body use="literal" namespace="http://example.com/stockquote.xsd"
                       encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
          </input>
          <output>
            <soap:body use="literal" namespace="http://example.com/stockquote.xsd"
                       encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
          </output>
        </soap:operation>
      </operation>
    </soap:binding>
  </binding>
这部分将服务访问点的抽象定义与SOAP HTTP绑定,描述如何通过SOAP/HTTP来访问按照前面描述的访问入口点类型部署的访问入口。其中规定了在具体SOAP调用时,应当使用的soapAction是"http://example.com/GetLastTradePrice",而请求/响应消息的编码风格都应当采用SOAP规范默认定义的编码风格" http://schemas.xmlsoap.org/soap/encoding/"。

<service name="StockQuoteService">
    <documentation>股票查询服务</documentation>
    <port name="StockQuotePort" binding="tns:StockQuoteBinding">
    <soap:address location="http://example.com/stockquote"/>
    </port>
  </service>

</definitions>
这部分是具体的Web服务的定义,在这个名为StockQuoteService的Web服务中,提供了一个服务访问入口,访问地址是"http://example.com/stockquote",使用的消息模式是由前面的binding所定义的。

按照这个WSDL文档的描述,在具体Web服务的使用中,具体发生的SOAP交互可能如下面所示:

SOAP消息请求:
POST /StockQuote HTTP/1.1
Host: example.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
SOAPAction: "http://example.com/GetLastTradePrice"

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <m:TradePriceRequest xmlns:m="http://example.com/stockquote.xsd">
      <tickerSymbol>MSFT</tickerSymbol >
    </m:TradePriceRequest>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

SOAP消息响应:

HTTP/1.1 200 OK
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  <SOAP-ENV:Body>
    <m:TradePriceResult xmlns:m=" http://example.com/stockquote.xsd ">
      <price>74.5</price>
    </m:TradePriceResult >
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>


你可能感兴趣的:(编程,xml,Web,网络协议,SOAP)