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 消息的基本格式:
<?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 文檔,包含如下元素:
Envelope
是 SOAP 消息結構的主要容器,也是 SOAP 消息的根元素,它必須出現在每個 SOAP 消息中,用于把此 XML 文檔標示為一條 SOAP 消息。
在 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 的 encodingStyle
屬性用于定義在文檔中使用的數據類型。此屬性可出現在任何 SOAP 元素中,并會被應用到元素的內容及元素的所有子元素上。
這個是可選的,如果需要添加 Header
元素,那么它必須是 Envelope
的第一個子元素。Header
還可以包含0個或多個可選的子元素,這些子元素稱為 Header 項,所有的 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>
Header
元素用于與消息一起傳輸一些附加的消息,如身份驗證信息等。
SOAP 消息的 Body
元素可以包含以下任何元素:
所有 Body
元素的直接子元素都稱為 Body 項,所有 Body 項一般是屬于某個特點的命名空間的。
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>
注:以上例子表示通過手機號獲取手機號歸屬地等信息。第一個例子是請求消息,第二個例子是它的響應消息。
Fault
元素用于在 SOAP 消息中傳輸錯誤及狀態信息。如果 SOAP 消息中包括 Fault
元素,它必須作為一個 Body
的子元素出現,而且至多出現一次。Fault
元素本身也包含有描述錯誤詳細信息的子元素。它包含以下子元素:faultcode
,faultstring
,faultactor
,detail
。
子元素 | 描述 |
---|---|
faultcode | 供識別故障的代碼 |
faultstring | 可供人閱讀的有關故障的說明 |
faultactor | 有關是誰引發故障的信息 |
detail | 有關涉及 Body 元素的應用程序專用錯誤信息 |
其中 faultcode
是每一條錯誤消息都會提供的元素,它的值一般是以下錯誤代碼之一:
錯誤代碼 | 描述 |
---|---|
VersionMismatch | 無效的 SOAP Envelope 命名空間 |
MustUnderstand | 無法理解 Header 中擁有屬性 mustUnderstand = 1 的子元素 |
Client | 消息結構錯誤,或包含了不正確的信息 |
Server | 服務器出現錯誤 |
注:以上關于 SOAP Fault 的描述不完全適用于 SOAP 1.2 版本。因為 SOAP 1.2 版本在返回錯誤信息時,Fault
的子元素及其內容已經有所不同。具體看下面的例子:
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)
新聞熱點
疑難解答