WSDL 端口学习笔记

1. 什么是 WSDL 端口?

在 Web 服务中,WSDL(Web Services Description Language)用于描述服务接口。一个 WSDL 文档包含了服务接口、操作(operation)、消息(message)、数据类型(type)等信息。其中,端口(port)是 WSDL 中的一种重要元素。

WSDL 端口定义了一组与网络地址有关的绑定信息,它将一个特定的协议地址绑定到服务接口上。也就是说,通过 WSDL 端口,我们可以将服务接口和具体的网络地址绑定在一起,方便客户端进行访问。

2. WSDL 端口的类型

WSDL 端口分为两种类型:SOAP 端口和 HTTP 端口。

2.1 SOAP 端口

SOAP(Simple Object Access Protocol)是一种基于 XML 的协议,它用于在计算机网络上交换结构化的信息。SOAP 端口是在 SOAP 协议上定义的一种 WSDL 端口。

使用 SOAP 端口访问 Web 服务时,通信双方会采用 SOAP 消息进行数据交换。这种方式适用于需要进行复杂数据类型传输的场景。

2.2 HTTP 端口

HTTP(Hypertext Transfer Protocol)是一种用于传输超文本的协议,它是 Web 服务中最常用的协议之一。HTTP 端口是在 HTTP 协议上定义的一种 WSDL 端口。

使用 HTTP 端口访问 Web 服务时,通信双方会采用 HTTP 报文进行数据交换。这种方式适用于只需要进行简单数据类型传输的场景。

3. 示例

下面是一个使用 WSDL 端口的示例:

xmlCopy Code
<definitions ...> <portType name="CalculatorPortType"> <operation name="add"> <input message="tns:addRequest"/> <output message="tns:addResponse"/> </operation> </portType> <binding name="CalculatorBinding" type="tns:CalculatorPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="add"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="CalculatorService"> <port name="CalculatorPort" binding="tns:CalculatorBinding"> <soap:address location="http://www.example.org/calculator"/> </port> </service> </definitions>

在上面的示例中,我们定义了一个名为 CalculatorPortType 的端口类型。该端口类型包含了一个名为 add 的操作,它有一个输入参数和一个返回值。

接着,我们定义了一个名为 CalculatorBinding 的绑定,它将 CalculatorPortType 绑定在 SOAP 协议上。同时,我们还定义了一个名为 CalculatorService 的服务,它包含了一个名为 CalculatorPort 的端口,该端口绑定在 CalculatorBinding 上,并且它的地址是 http://www.example.org/calculator。

4. 总结

WSDL 端口是 Web 服务中的重要组成部分,它将服务接口和具体的网络地址绑定在一起,方便客户端进行访问。WSDL 端口分为 SOAP 端口和 HTTP 端口两种类型,分别适用于不同的数据传输场景。使用 WSDL 端口可以使得 Web 服务的访问更加方便和灵活。