XML SOAP
- SOAP 代表 **S**imple **O**bject **A**ccess **P**rotocol (简单对象访问协议)
- SOAP 是一种应用程序通信协议
- SOAP 是一种用于发送和接收消息的格式
- SOAP 是平台无关的
- SOAP 基于 XML
- SOAP 是 W3C 的一项推荐
为什么要使用 SOAP?
Web 应用程序能够通过 Internet 进行通信非常重要。
应用程序之间通信的最佳方式是 HTTP,因为所有 Internet 浏览器和服务器都支持 HTTP。SOAP 的创建就是为了实现这一目标。
SOAP 提供了一种应用程序在不同操作系统、使用不同技术和编程语言之间进行通信的方法。
SOAP 构建块
SOAP 消息是一个普通的 XML 文档,包含以下元素
- Envelope 元素,用于标识 XML 文档为 SOAP 消息
- Header 元素,包含头部信息
- Body 元素,包含调用和响应信息
- Fault 元素,包含错误和状态信息
以上所有元素都在 SOAP envelope 的默认命名空间中声明
http://www.w3.org/2003/05/soap-envelope
SOAP 编码和数据类型的默认命名空间是
http://www.w3.org/2003/05/soap-encoding
语法规则
以下是一些重要的语法规则
- SOAP 消息 **必须** 使用 XML 进行编码
- SOAP 消息 **必须** 使用 SOAP Envelope 命名空间
- SOAP 消息 **不得** 包含 DTD 引用
- SOAP 消息 **不得** 包含 XML 处理指令
Skeleton SOAP 消息
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
...
</soap:Header>
<soap:Body>
...
<soap:Fault>
...
</soap:Fault>
</soap:Body>
</soap:Envelope>
SOAP Envelope 元素
必需的 SOAP Envelope 元素是 SOAP 消息的根元素。此元素将 XML 文档定义为 SOAP 消息。
示例
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
...
消息信息在此处
...
</soap:Envelope>
xmlns:soap 命名空间
请注意上面示例中的 xmlns:soap 命名空间。它应始终具有值:"http://www.w3.org/2003/05/soap-envelope"。
该命名空间将 Envelope 定义为 SOAP Envelope。
如果使用不同的命名空间,应用程序将生成错误并丢弃该消息。
encodingStyle 属性
encodingStyle 属性用于定义文档中使用的数据类型。此属性可以出现在任何 SOAP 元素上,并且适用于该元素的以及所有子元素的名称。
SOAP 消息没有默认编码。
语法
soap:encodingStyle="URI"
示例
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
...
消息信息在此处
...
</soap:Envelope>
SOAP Header 元素
可选的 SOAP Header 元素包含关于 SOAP 消息的应用程序特定信息(如身份验证、付款等)。
如果存在 Header 元素,它必须是 Envelope 元素的第一个子元素。
注意: Header 元素的所有直接子元素都必须是命名空间限定的。
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://w3schools.org.cn/transaction/"
soap:mustUnderstand="1">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
上面的示例包含一个带有 "Trans" 元素的头部,一个值为 1 的 "mustUnderstand" 属性,以及值为 234 的内容。
SOAP 在默认命名空间中定义了三个属性。这些属性是:mustUnderstand、actor 和 encodingStyle。
在 SOAP Header 中定义的属性决定了接收者应如何处理 SOAP 消息。
mustUnderstand 属性
SOAP mustUnderstand 属性可用于指示头部条目对接收者来说是必需的还是可选的。
如果将 mustUnderstand="1" 添加到 Header 元素的子元素中,则表示处理 Header 的接收者必须识别该元素。如果接收者不识别该元素,它在处理 Header 时会失败。
语法
soap:mustUnderstand="0|1"
示例
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://w3schools.org.cn/transaction/"
soap:mustUnderstand="1">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
actor 属性
SOAP 消息可能从发送方到接收方,在消息路径上传递不同的端点。但是,并非 SOAP 消息的所有部分都可能针对最终端点,而是可能针对消息路径上一个或多个端点。
SOAP actor 属性用于将 Header 元素指向特定的端点。
语法
soap:actor="URI"
示例
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://w3schools.org.cn/transaction/"
soap:actor="https://w3schools.org.cn/code/">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
encodingStyle 属性
encodingStyle 属性用于定义文档中使用的数据类型。此属性可以出现在任何 SOAP 元素上,并且适用于该元素的以及所有子元素的名称。
SOAP 消息没有默认编码。
语法
soap:encodingStyle="URI"
SOAP Body 元素
必需的 SOAP Body 元素包含发送给消息最终端点的实际 SOAP 消息。
SOAP Body 元素的直接子元素可以是命名空间限定的。
示例
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<m:GetPrice xmlns:m="https://w3schools.org.cn/prices">
<m:Item>Apples</m:Item>
</m:GetPrice>
</soap:Body>
</soap:Envelope>
上面的示例请求苹果的价格。请注意,上面的 m:GetPrice 和 Item 元素是应用程序特定的元素。它们不是 SOAP 命名空间的一部分。
SOAP 响应可能如下所示
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<m:GetPriceResponse xmlns:m="https://w3schools.org.cn/prices">
<m:Price>1.90</m:Price>
</m:GetPriceResponse>
</soap:Body>
</soap:Envelope>
SOAP Fault 元素
可选的 SOAP Fault 元素用于指示错误消息。
SOAP Fault 元素包含 SOAP 消息的错误和状态信息。
如果存在 Fault 元素,它必须作为 Body 元素的子元素出现。一个 Fault 元素在一个 SOAP 消息中只能出现一次。
SOAP Fault 元素具有以下子元素
子元素 | 描述 |
---|---|
<faultcode> | 用于识别错误的代号 |
<faultstring> | 对错误的易于理解的解释 |
<faultactor> | 关于谁导致了错误的发生的信息 |
<detail> |
包含与 Body 元素相关的应用程序特定的错误信息 |
SOAP 故障代码
在描述故障时,必须在 faultcode 元素中使用下面定义的 faultcode 值
Error | 描述 |
---|---|
VersionMismatch | 发现了 SOAP Envelope 元素的无效命名空间 |
MustUnderstand | Header 元素的直接子元素,其 mustUnderstand 属性设置为 "1",但未被理解 |
Client | 消息格式不正确或包含不正确的信息 |
Server | 服务器出现问题,导致消息无法继续 |
HTTP 协议
HTTP 通过 TCP/IP 进行通信。HTTP 客户端使用 TCP 连接到 HTTP 服务器。建立连接后,客户端可以向服务器发送 HTTP 请求消息
POST /item HTTP/1.1
Host: 189.123.255.239
Content-Type: text/plain
Content-Length: 200
然后服务器处理请求,并将 HTTP 响应发送回客户端。响应包含一个状态码,指示请求的状态
200 OK
Content-Type: text/plain
Content-Length: 200
在上面的示例中,服务器返回了状态码 200。这是 HTTP 的标准成功代码。
如果服务器无法解码请求,它可能会返回类似这样的内容
400 Bad Request
Content-Length: 0
SOAP 绑定
SOAP 规范定义了 SOAP 消息的结构,而不是如何交换它们。这个空白由所谓的“SOAP 绑定”来填补。SOAP 绑定是允许使用传输协议有效交换 SOAP 消息的机制。
大多数 SOAP 实现都提供了常见传输协议(如 HTTP 或 SMTP)的绑定。
HTTP 是同步的,并且被广泛使用。SOAP HTTP 请求至少指定两个 HTTP 头部:Content-Type 和 Content-Length。
SMTP 是异步的,并且用于最后手段或特定情况。
SOAP 的 Java 实现通常为 JMS (Java Messaging System) 协议提供特定的绑定。
Content-Type
SOAP 请求和响应的 Content-Type 头部定义了消息的 MIME 类型以及用于请求或响应的 XML 主体的字符编码(可选)。
语法
Content-Type: MIMEType; charset=character-encoding
示例
POST /item HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Content-Length
SOAP 请求和响应的 Content-Length 头部指定了请求或响应正文中的字节数。
语法
Content-Length: bytes
示例
POST /item HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 250
SOAP 示例
在下面的示例中,将 GetStockPrice 请求发送到服务器。请求有一个 StockName 参数,以及一个将在响应中返回的 Price 参数。函数命名空间定义在 "http://www.example.org/stock" 中。
SOAP 请求
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:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<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:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPriceResponse>
<m:Price>34.5</m:Price>
</m:GetStockPriceResponse>
</soap:Body>
</soap:Envelope>