SOAP教程

1,SOAP 是一种简单的基于 XML 的协议,它使应用程序通过 HTTP 来交换信息;

SOAP是基于XML的简易协议,可以使应用程序在Http之上进行信息交换;;

SOAP是用于访问网络服务的协议;

2,SOAP:简易对象访问协议,通信协议,用于应用程序之间通信,用于发送消息的格式;用来通过互联网通信,独立于平台,独立于语言,基于XML,可扩展,可以绕过防火墙,将被作为W3C标准;

3,应用程序之间通过互联网通信很重要;

之前是通过远程调用(RPC)在应用之间通信,但是Http不是为此设计的,RPC会产生兼容性问题,防火墙和代理服务器通常会阻止此类流量。

通过HTTP在应用程序之间通信是更好的办法,因为HTTP得到所有网络浏览器和服务器的支持,SOAP就是用来完成这个任务的。

SOAP提供了一种标准,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信。

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

4,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

5,语法规则:

SOAP消息必须:

用XML编码,使用SOAP Envelope 命名空间,使用 SOAP Encoding 命名空间,不能包含DTD引用,不能包含XML处理指令;

6,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>

Web services 使用 XML 来编解码数据,并使用 SOAP 来传输数据。

7,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 元素。

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

encodingStyle属性:

SOAP 的 encodingStyle 属性用于定义在文档中使用的数据类型。此属性可出现在任何 SOAP 元素中,并会被应用到元素的内容及元素的所有子元素上。SOAP 消息没有默认的编码方式。

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>

8,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 消息进行处理。

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.w3school.com.cn/transaction/"
soap:actor="http://www.w3school.com.cn/appml/">
234
</m:Trans>
</soap:Header>

...
...

</soap:Envelope>

mustUnderstand 属性

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

假如您向 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.w3school.com.cn/transaction/"
soap:mustUnderstand="1">
234
</m:Trans>
</soap:Header>

...
...

</soap:Envelope>

encodingStyle 属性

 

 

 

 

 

posted @ 2014-02-27 17:54  教程学习  阅读(341)  评论(0)    收藏  举报