SOAP 介绍
简介SOAP(Simple Object Access Protoco)简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于 XML 的协议。此协议规范由 IBM、Microsoft、UserLand 和 DevelopMentor 在1998年共同提出,并得到 IBM,莲花(Lotus),康柏(Compaq)等公司的支持,于2000年提交给万维网联盟(World Wide Web Consortium;W3C)。现在,SOAP 协议规范由万维网联盟的 XML工作组维护。SOAP 1.2 版在2003年6月24日成为 W3C 的推荐版本。 SOAP 协议包括以下四个部分的内容:
四个部分之间的关系SOAP 消息基本上是从发送端到接收端的单向传输,但它们常常结合起来执行类似于请求 / 应答的模式。所有的 SOAP 消息都使用 XML 编码。一条 SOAP 消息就是一个包含有一个必需的 SOAP 的封装包,一个可选的 SOAP 标头(Header)和一个必需的 SOAP 体块(Body)的 XML 文档。 把 SOAP 绑定到 HTTP 提供了同时利用 SOAP 的样式和分散的灵活性的特点以及 HTTP 的丰富的特征库的优点。在HTTP上传送 SOAP 并不是说 SOAP 会覆盖现有的 HTTP 语义,而是 HTTP 上的 SOAP 语义会自然的映射到 HTTP 语义。在使用 HTTP 作为协议绑定的场合中, RPC 请求映射到 HTTP 请求上,而 RPC 应答映射到 HTTP 应答。然而,在 RPC 上使用 SOAP 并不仅限于 HTTP 协议绑定。SOAP也可以绑定到TCP和UDP协议上。 虽然这四个部分都作为 SOAP 的一部分,作为一个整体定义的,但他们在功能上是相交的、彼此独立的。特别的,信封(envelop)和编码规则(encoding rules)是被定义在不同的 XML 命名空间中,这样使得定义更加简单。 语法规则
SOAP 消息格式SOAP 消息的格式比较简单,如下图: 下面是一条 SOAP 消息的基本格式: <?xml version="1.0" encoding="utf-8"?> <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 消息就是一个普通的 XML 文档,包含如下元素:
语法规则详解SOAP Envelope
在 SOAP 中,使用命名空间将 SOAP 消息元素与应用程序自定义的元素区分开来,将 SOAP 消息元素的作用域限制在一个特定的区域。 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> </soap:Envelope> SOAP 的 SOAP Header这个是可选的,如果需要添加 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> <soap:Header> <AuthenHeader xmlns="http://www.example.com"> <sAuthenticate>string</sAuthenticate> </AuthenHeader> </soap:Header> <soap:Body> </soap:Body> </soap:Envelope>
SOAP BodySOAP 消息的
所有 SOAP 请求消息例子: <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> <soap:Body> <getMobileCodeInfo xmlns="http://www.example.com"> <mobileCode>string</mobileCode> <userID>string</userID> </getMobileCodeInfo> </soap:Body> </soap:Envelope> SOAP 响应消息例子: <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> <soap:Body> <getMobileCodeInfoResponse xmlns="http://www.example.com"> <getMobileCodeInfoResult>string</getMobileCodeInfoResult> </getMobileCodeInfoResponse> </soap:Body> </soap:Envelope> 注:以上例子表示通过手机号获取手机号归属地等信息。第一个例子是请求消息,第二个例子是它的响应消息。 SOAP Fault
其中
注:以上关于 SOAP Fault 的描述不完全适用于 SOAP 1.2 版本。因为 SOAP 1.2 版本在返回错误信息时, SOAP v1.1 错误消息例子: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body> <soap:Fault> <faultcode>soap:Client</faultcode> <faultstring>Input string was not in a correct format.</faultstring> <detail/> </soap:Fault> </soap:Body> </soap:Envelope> SOAP v1.2 错误消息例子: <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> <soap:Body> <soap:Fault> <soap:Code> <soap:Value>soap:Sender</soap:Value> </soap:Code> <soap:Reason> <soap:Text xml:lang="en">Input string was not in a correct format.</soap:Text> </soap:Reason> <soap:Detail/> </soap:Fault> </soap:Body> </soap:Envelope> 从以上返回结果来看,其实所返回的错误信息内容并没有太多改变,只是 XML 的元素发生了一些变化,具体还需要读者自己理解。 总结本文章节有点乱,还算是把 SOAP 的基本知识点都过了一遍。接下来,我将会结合一些实例,进一步说明 SOAP 的特点以及如何使用 SOAP Web 服务。(本文已存档 Github) 参考文献
其他
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |