加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService之SOAP

发布时间:2020-12-16 22:33:51 所属栏目:安全 来源:网络整理
导读:什么是 SOAP? SOAP 指 简易对象访问协议 SOAP 是一种 通信协议 SOAP 用于 应用程序之间 的通信 SOAP 是一种用于 发送消息 的格式 SOAP 被设计用来 通过因特网 进行通信 SOAP 独立于平台 SOAP 独立于语言 SOAP 基于 XML SOAP 很简单并可扩展 SOAP 允许您 绕

什么是 SOAP?

  • SOAP 指简易对象访问协议

  • SOAP 是一种通信协议

  • SOAP 用于应用程序之间的通信

  • SOAP 是一种用于发送消息的格式

  • SOAP 被设计用来通过因特网进行通信

  • SOAP独立于平台

  • SOAP独立于语言

  • SOAP基于 XML

  • SOAP很简单并可扩展

  • SOAP 允许您绕过防火墙

  • SOAP 将被作为W3C 标准来发展

SOAP 构建模块

一条 SOAP 消息就是一个普通的 XML 文档,包含下列元素:

  • 必需的 Envelope 元素,可把此 XML 文档标识为一条 SOAP 消息

  • 可选的 Header 元素,包含头部信息

  • 必需的 Body 元素,包含所有的调用和响应信息

  • 可选的 Fault 元素,提供有关在处理此消息所发生错误的信息

所有以上的元素均被声明于针对 SOAP 封装的默认命名空间中:

http://www.w3.org/2001/12/soap-envelope

以及针对 SOAP 编码和数据类型的默认命名空间:

http://www.w3.org/2001/12/soap-encoding


SOAP 消息的基本结构

<?xml?version="1.0"?>??
<soap:Envelope??
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"??
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">??
??
<soap:Header>??
??...??
??...??
</soap:Header>??
??
<soap:Body>??
??...??
??...??
??<soap:Fault>??
????...??
????...??
??</soap:Fault>??
</soap:Body>??
??
</soap:Envelope>

强制使用的 SOAP 的 Envelope 元素是 SOAP 消息的根元素。

SOAP Envelope 元素

必需的 SOAP 的 Envelope 元素是 SOAP 消息的根元素。它可把 XML 文档定义为 SOAP 消息。

请注意 xmlns:soap 命名空间的使用。它的值应当始终是:

http://www.w3.org/2001/12/soap-envelope

并且它可把封装定义为 SOAP 封装:

<?xml?version="1.0"?>??
<soap:Envelope??
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"??
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">??
??...??
??Message?information?goes?here??
??...??
</soap:Envelope>

xmlns:soap 命名空间

SOAP 消息必须拥有与命名空间 "http://www.w3.org/2001/12/soap-envelope" 相关联的一个 Envelope 元素。

如果使用了不同的命名空间,应用程序会发生错误,并抛弃此消息。


SOAP Header 元素

可选的 SOAP Header 元素可包含有关 SOAP 消息的应用程序专用信息(比如认证、支付等)。如果 Header 元素被提供,则它必须是 Envelope 元素的第一个子元素。

注释:所有 Header 元素的直接子元素必须是合格的命名空间。

<?xml?version="1.0"?>??
<soap:Envelope??
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"??
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">??
??
<soap:Header>??
<m:Trans?xmlns:m="http://www.w3school.com.cn/transaction/"?soap:mustUnderstand="1">234</m:Trans>??
</soap:Header>??
??
...??
...??
??
</soap:Envelope>

上面的例子包含了一个带有一个 "Trans" 元素的头部,它的值是 234,此元素的 "mustUnderstand" 属性的值是 "1"。

SOAP 在默认的命名空间中 ("http://www.w3.org/2001/12/soap-envelope") 定义了三个属性。这三个属性是:actor、 mustUnderstand 以及 encodingStyle。这些被定义在 SOAP 头部的属性可定义容器如何对 SOAP 消息进行处理。


SOAP Body 元素

必需的 SOAP Body 元素可包含打算传送到消息最终端点的实际 SOAP 消息。

SOAP Body 元素的直接子元素可以是合格的命名空间。SOAP 在默认的命名空间中("http://www.w3.org/2001/12/soap-envelope")定义了 Body 元素内部的一个元素。即 SOAP 的 Fault 元素,用于指示错误消息。

<?xml?version="1.0"?>??
<soap:Envelope??
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"??
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">??
??
<soap:Body>??
???<m:GetPrice?xmlns:m="http://www.w3school.com.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/2001/12/soap-envelope"??
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">??
??
<soap:Body>??
???<m:GetPriceResponse?xmlns:m="http://www.w3school.com.cn/prices">??
??????<m:Price>1.90</m:Price>??
???</m:GetPriceResponse>??
</soap:Body>??
??
</soap:Envelope>

一个 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/2001/12/soap-envelope"??
soap:encodingStyle="http://www.w3.org/2001/12/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/2001/12/soap-envelope"??
soap:encodingStyle="http://www.w3.org/2001/12/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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读