参考:【接口开发】浅谈 SOAP Webserver 与 Restful Webserver 区别
目录
客户端和服务器端的通讯方式:
一、Web Service
Web Service服务通常被定义为一组模块化的API,它们可以通过网络进行调用,来执行远程系统的请求服务。Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。各应用程序通过网络协议和规定的一些标准数据格式(Http,XML,Soap)来访问Web Service,通过Web Service内部执行得到所需结果。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。
实际上,Web Service的主要目标是跨平台的可互操作性。为了达到这一目标,Web Service完全基于XML(可扩展标记语言)、XSD(XMLSchema)等独立于平台、独立于软件供应商的标准,是创建可互操作的、分布式应用程序的新平台。Web service平台必须提供一套标准的类型系统,用于沟通不同平台、编程语言和组件模型中的不同类型系统。
1. 基本元素
Web Service的三种基本元素:SOAP、WSDL和UDDI。
SOAP 是一种基于XML协议、用于分散或分布的环境中交换信息的简单协议。
WSDL ( 网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及如何对它们进行访问。web service 描述语言 (WSDL) 就是这样一个基于 XML 的语言,用于描述 web service 及其函数、参数和返回值。因为是基于 XML 的,既可供人阅读,也可使用工具生成调用相应 web service 的代码。实例如下:
<?xml version="1.0" encoding="UTF-8" ?> <definitions targetNamespace="http://schemas.xmlsoap.org/HelloService" xmlns:tns="http://schemas.xmlsoap.org/HelloService" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <!-- (参数类型) 数据类型定义,一般使用XML Schema中的类型系统 --> <types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.xmlsoap.org/HelloService" attributeFormDefault="qualified" elementFormDefault="qualified" > <xsd:element name="sayHello"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="name" nillable="true" type="xsd:string" /> </xsd:sequence></xsd:complexType></xsd:element> <xsd:element name="sayHelloResponse"> <xsd:complexType> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="name" nillable="true" type="xsd:string" /> </xsd:sequence></xsd:complexType></xsd:element> </xsd:schema> </types> <!-- (操作中的参数) 使用Types所定义的类型来定义整个消息的数据结构 --> <message name="sayHelloRequest"> <part name="parameters" element="tns:sayHello" /> </message> <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse" /> </message> <!-- (提供的操作) portType描述了一组操作 --> <portType name="HelloServicePortType"> <operation name="sayHello"> <input name="sayHelloRequest" message="tns:sayHelloRequest" /> <output name="sayHelloResponse" message="tns:sayHelloResponse" /> </operation> </portType> <!-- (绑定通信协议)描述如何将portType映射成传输/消息传递协议,binding将portType绑定到特定的协议(例如 SOAP、HTTP或MIME)--> <binding name="HelloServicePortBinding" type="tns:HelloServicePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <operation name="sayHello"> <!-- 元素的soapAction属性被转换成HTTP头 --> <soap:operation soapAction="" /> <input name="sayHelloRequest"> <soap:body use="literal" /> </input> <output name="sayHelloResponse"> <soap:body use="literal" /> </output> </operation> </binding> <!-- (有特定操作的服务)service列出某个特定绑定的连接信息。服务可以有一个或多个端口,每个端口都定义一个不同的连接方法(例如HTTP/SMTP等等) --> <service name="HelloService"> <port name="HelloServiceHttpPort" binding="tns:HelloServicePortBinding"> <soap:address location="http://localhost:8080/services/HelloService" /> </port> </service> </definitions>