3.SOAP
1.SOAP消息结构
由Envelope(信封)、Header(头部)、Body(正文)和Falut(错误信息)组成,其中Header和Falut是可选的。
如下为一个SOAP请求,向服务器请求IBM的股票价格:
POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body xmlns:m="http://www.example.org/stock"><m:GetStockPrice><m:StockName>IBM</m:StockName></m:GetStockPrice></soap:Body>
</soap:Envelope>
如下为SOAP的响应
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body xmlns:m="http://www.example.org/stock"><m:GetStockPriceResponse><m:Price>34.5</m:Price></m:GetStockPriceResponse></soap:Body>
</soap:Envelope>
Content-Type和Content-Length都是必须的,SOAP 的请求和响应的Content-Length头规定请求或响应主体的字节数。
其中xmlns:soap="http://www.w3.org/2003/05/soap-envelope"表示命名空间,通过命名空间可以指明Envelope属于哪个标准或协议,可能会决定该如何解析下边的元素
2.WSDL
WSDL (Web Services Description Language,Web服务描述语言)是一种XML Application
2.1XML命名空间
在一个XML中,如果有两个同名的元素,但代表的意思又不同,此时可以使用命名空间来区分:
如下有两个table元素,第一个元素代表了表格"table",第二个元素代表了桌子"table":
<root><table><tr><td>Apples</td><td>Bananas</td></tr></table><table><name>African Coffee Table</name><width>80</width><length>120</length></table>
</root>
因此使用命名空间进行区分:
命名空间可以通过元素开始标记中的 xmlns 属性来定义。
命名空间声明使用这种语法:xmlns:prefix=“URI”。
<root><h:table xmlns:h="http://www.w3.org/TR/html4/"><h:tr><h:td>Apples</h:td><h:td>Bananas</h:td></h:tr></h:table><f:table xmlns:f="https://www.w3school.com.cn/furniture"><f:name>African Coffee Table</f:name><f:width>80</f:width><f:length>120</f:length></f:table>
</root>
2.2 XML Schema
XML Schema是一种结构良好的XML编写方式
如下XML:
<?xml version="1.0"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
使用XML Schema来编写:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified"><xs:element name="note"><xs:complexType><xs:sequence><xs:element name="to" type="xs:string"/><xs:element name="from" type="xs:string"/><xs:element name="heading" type="xs:string"/><xs:element name="body" type="xs:string"/></xs:sequence></xs:complexType></xs:element>
</xs:schema>
note 元素是一个复合类型,因为它包含其他的子元素。其他元素 (to, from, heading, body) 是简易类型,因为它们没有包含其他元素。
2.3WSDL文档结构
元素是最重要的 WSDL 元素。
它可描述一个 web service、可被执行的操作,以及相关的消息。
可以把 元素比作传统编程语言中的一个函数库(或一个模块、或一个类)。
元素定义一个操作的数据元素。
每个消息均由一个或多个部件组成。类似于传统编程语言的形参
元素定义 web service 使用的数据类型。
为了最大程度的平台中立性,WSDL 使用 XML Schema 语法来定义数据类型。
元素为每个端口定义消息格式和协议细节。
详情:https://www.w3school.com.cn/wsdl/wsdl_documents.asp