SOAP教程

SOAP教程

内容

  • SOAP简介

  • SOAP语法

  • SOAP Envelope元素

  • SOAP Header元素

  • SOAP Body元素

  • SOAP Fault元素

  • SOAP HTTP Binding


SOAP简介

简单的基于 XML 的协议,它使应用程序通过 HTTP 来交换信息

Microsoft 和 SOAP

SOAP 是微软 .net 架构的关键元素,用于未来的因特网应用程序开发

SOAP语法

构建模块

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

(两必选两可选)

  • 必须的元素

    • Envelope元素---把此XML文档标识为一条SOAP消息

    • Body元素---包含所有的调用和响应消息

  • 可选元素

    • Header元素---包含头信息

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

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

语法规则

这里是一些重要的语法规则:

  • SOAP 消息必须用 XML 来编码

  • SOAP 消息必须使用 SOAP Envelope 命名空间

  • SOAP 消息必须使用 SOAP Encoding 命名空间

  • SOAP 消息不能包含 DTD 引用

  • SOAP 消息不能包含 XML 处理指令

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 的 Envelope 元素是 SOAP 消息的根元素

必需的 SOAP 的 Envelope 元素是 SOAP 消息的根元素。它可把 XML 文档定义为 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>
encodingStyle属性

SOAP 的 encodingStyle 属性用于定义在文档中使用的数据类型

此属性可出现在任何 SOAP 元素中,并会被应用到元素的内容及元素的所有子元素上

SOAP 消息没有默认的编码方式

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

  • 使用了不同的命名空间,应用程序会发生错误,并抛弃此消息---是否会返回提示

语法:

soap:encodingStyle="URI"

实例:

<?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>

SOAP Body元素

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

SOAP Body 元素的直接子元素可以是合格的命名空间。

实例:

<?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.w3schools.com/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.w3schools.com/prices">
   <m:Price>1.90</m:Price>
 </m:GetPriceResponse>
</soap:Body>

</soap:Envelope>

非必带元素

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

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.w3schools.com/transaction/"
 soap:mustUnderstand="1">234
 </m:Trans>
</soap:Header>
...
...
</soap:Envelope>
Header的三个默认元素
mustUnderstan属性

用于标识标题项对于要对其进行处理的接收者来说是强制的还是可选的

Header 元素的某个子元素添加了 "mustUnderstand="1",则它可指示处理此头部的接收者必须认可此元素。假如此接收者无法认可此元素,则在处理此头部时必须失效

实例:

soap:mustUnderstand="0|1"

实例:

<?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.w3schools.com/transaction/"
 soap:mustUnderstand="1">234
 </m:Trans>
</soap:Header>
...
...
</soap:Envelope>
actor属性

沿着消息路径经过不同的端点,SOAP 消息可从某个发送者传播到某个接收者

并非 SOAP 消息的所有部分均打算传送到 SOAP 消息的最终端点,也许打算传送给消息路径上的一个或多个端点

SOAP 的 actor 属性可被用于将 Header 元素寻址到一个特定的端点

语法:

soap:actor="URI"

实例:

<?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.w3schools.com/transaction/"
 soap:actor="http://www.w3schools.com/appml/">234
 </m:Trans>
</soap:Header>
...
...
</soap:Envelope>
encodingStyle属性

语法:

soap:encodingStyle="URI"

SOAP Fault元素

用于存留 SOAP 消息的错误和状态信息

作用:

用于指示错误消息

重点:

  • 如果已提供了 Fault 元素,则它必须是 Body 元素的子元素。

  • 在一条 SOAP 消息中,Fault 元素只能出现一次

Fault的子元素

子元素描述
<faultcode> 供识别故障的代码
<faultstring> 可供人阅读的有关故障的说明
<faultactor> 有关是谁引发故障的信息
<detail> 存留涉及 Body 元素的应用程序专用错误信息
SOAP Fault 代码

作用:

用于描述错误时的 faultcode 元素

错误描述
VersionMismatch SOAP Envelope 元素的无效命名空间被发现
MustUnderstand Header 元素的一个直接子元素(带有设置为 "1" 的 mustUnderstand 属性)无法被理解。
Client 消息被不正确地构成,或包含了不正确的信息。
Server 服务器有问题,因此无法处理进行下去。

SOAP HTTP Binding

HTTP协议

在 TCP/IP 之上进行通信。HTTP 客户机使用 TCP 连接到 HTTP 服务器。在建立连接之后,客户机可向服务器发送 HTTP 请求消息

实例:

//发送请求
POST /item HTTP/1.1
Host: 189.123.255.239---IP地址
Content-Type: text/plain---内容类型
Content-Length: 200---内容长度

服务器会处理此请求,然后向客户机发送一个 HTTP 响应

实例:

200 OK---请求状态OK,状态代码200
Content-Type: text/plain
Content-Length: 200

服务器无法对请求进行解码,它可能会返回类似这样的信息

400 Bad Request
Content-Length: 0

SOAP HTTP Binding

SOAP 方法指的是遵守 SOAP 编码规则的 HTTP 请求/响应:

SOAP = HTTP + XML

SOAP请求种类:

  • HTTP POST

  • HTTP GET

HTTP POST 请求规定至少两个 HTTP 头:

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

作用:

  • 规定请求或响应主体的字节数

语法:

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/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>

 

posted @ 2021-04-17 16:30  俊king  阅读(211)  评论(0)    收藏  举报