尽管没有出现在图7-1中,但WSDL是Web服务概念的中心。它被看做Web服务的核心组件、服务发布和输出接口及功能信息的机制。WSDL一般通过一个或者多个页面实现,这些页面可以在服务器上Web服务驻留的位置访问(一般使用.wsdl和.xsd文件扩展名)。
W3C的WSDL规范将其描述为“一种XML语法,用于将网络服务描述为一组能够交换消息的通信端点。”本质上,这意味着WSDL文档描述Web服务输出的功能(操作)以及连接(绑定)的方式。继续前面讨论过的SOAP示例,下面是一个提供股票交易功能的简单Web服务的WSDL定义示例。注意,我们的例子包含了服务的下列关键信息:
·Types(类型)和Message(消息)元素定义可以传递的消息格式(通过嵌入式XML schema定义)。
·portType元素定义消息传递的语义,例如request-only(仅请求)、request-response(请求-响应)和response-only(仅响应)。
·Binding(绑定)元素指定具体传输方法(如HTTP、HTTPS或者SMTP)之上的各种编码。
·Service(服务)元素定义服务的端点(一个URL)。
<?xml version="1.0"?> <definitions name="StockTrader" targetNamespace="http://stocktrader.edu/stockquote.wsdl" xmlns:tns="http://stocktrader.edu/stockquote.wsdl" xmlns:xsd1="http://stocktrader.edu/stockquote.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types> <schema targetNamespace="http://stocktrader.edu/stockquote.xsd" xmlns="http://www.w3.org/2000/10/XMLSchema"> <element name="GetQuote"> <complexType> <all> <element name="tickerSymbol" type="string"/> </all> </complexType> </element> <element name="Price"> <complexType> <all> <element name="price" type="float"/> </all> </complexType> </element> </schema> </types> <message name="GetQuoteInput"> <part name="body" element="xsd1:QuoteRequest"/> </message> <message name="GetQuoteOutput"> <part name="body" element="xsd1:StockPrice"/> </message> <portType name="StockQuotePortType"> <operation name="GetQuote"> <input message="tns:GetQuoteInput "/> <output message="tns:GetQuoteOutput "/> </operation> </portType> <binding name="StockQuoteSoapBinding" type="tns:StockQuotePortType"> <soap:binding transport="http:// schemas.xmlsoap.org/soap/http"/> <operation name="GetQuote"> <soap:operation soapAction= "http://stocktrader.edu/GetQuote"/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="StockQuoteService"> <documentation>User-readable documentation here </documentation> <port name="StockQuotePort" binding="tns:StockQuoteBinding"> <soap:address location= "http://stocktrader.edu/stockquote"/> </port> </service> </definitions>
WSDL文档中的信息一般是无害的,因为它通常用于公开消费。但是,正如你所看到的,如果没有适当的安全措施,大量业务逻辑可能会为WSDL所暴露。实际上,WSDL文档通常链接到“接口契约”,接口契约描述一个事务中愿意接受的特定业务。此外,Web开发人员有着在WSDL文档这样的应用文件中放入不恰当信息的坏名声,我们也确实发现通过这个接口可以收获许多泄露的信息。