📜  WSDL教程(1)

📅  最后修改于: 2023-12-03 15:21:16.268000             🧑  作者: Mango

WSDL教程

WSDL (Web Services Description Language) 是一种 XML 格式的文件,用于描述 Web 服务的功能、参数和方法。

WSDL的作用

WSDL 的主要作用是:

  1. 描述 Web 服务的功能
  2. 描述 Web 服务的参数
  3. 描述 Web 服务的方法
WSDL的结构

WSDL 文件包含以下几个部分:

  • Types:定义数据类型,通常是 XML Schema 定义
  • Messages:描述消息
  • PortType:定义一组操作
  • Bindings:将端口类型(PortType)映射成具体的传输协议,如 SOAP、HTTP 等
  • Service:定义服务的位置
WSDL的示例

以下是一个简单的 WSDL 示例:

<?xml version="1.0"?>
<definitions name="MyService"
   targetNamespace="http://www.example.org/MyService"
   xmlns:tns="http://www.example.org/MyService"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns="http://schemas.xmlsoap.org/wsdl/">
   
   <message name="GetStockPriceInput">
      <part name="tickerSymbol" type="xsd:string"/>
   </message>

   <message name="GetStockPriceOutput">
      <part name="price" type="xsd:float"/>
   </message>

   <portType name="StockQuotePortType">
      <operation name="GetStockPrice">
         <input message="tns:GetStockPriceInput"/>
         <output message="tns:GetStockPriceOutput"/>
      </operation>
   </portType>

   <binding name="StockQuoteBinding" type="tns:StockQuotePortType">
      <soap:binding style="rpc"
         transport="http://schemas.xmlsoap.org/soap/http"/>
      <operation name="GetStockPrice">
         <soap:operation soapAction="http://example.com/GetStockPrice"/>
         <input><soap:body use="encoded" namespace="http://example.com"
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></input>
         <output><soap:body use="encoded" namespace="http://example.com"
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></output>
      </operation>
   </binding>

   <service name="StockQuoteService">
      <port name="StockQuotePort" binding="tns:StockQuoteBinding">
         <soap:address location="http://example.com/stockquote"/>
      </port>
   </service>
</definitions>

以上 WSDL 定义了一个名为 "MyService" 的服务,服务的命名空间为 "http://www.example.org/MyService"。

服务包括一个名为 "GetStockPrice" 的操作,用于获取指定股票代码的股票价格。此操作使用 SOAP/RPC 样式和 HTTP 协议进行传输。

总结

WSDL 是一种用于描述 Web 服务的功能、参数和方法的 XML 文件。它定义了 Web 服务的结构,包括类型、消息、端口类型、绑定和服务。学习 WSDL 对于开发 Web 服务至关重要。