XML SOAP
- SOAP 代表 **S**imple **O**bject **A**ccess **P**rotocol (簡單物件訪問協議)
- SOAP 是一種應用程式通訊協議
- SOAP 是一種用於傳送和接收訊息的格式
- SOAP 是平臺無關的
- SOAP 基於 XML
- SOAP 是 W3C 的一項推薦
為什麼要使用 SOAP?
Web 應用程式能夠透過 Internet 進行通訊非常重要。
應用程式之間通訊的最佳方式是 HTTP,因為所有 Internet 瀏覽器和伺服器都支援 HTTP。SOAP 的建立就是為了實現這一目標。
SOAP 提供了一種應用程式在不同作業系統、使用不同技術和程式語言之間進行通訊的方法。
SOAP 構建塊
SOAP 訊息是一個普通的 XML 文件,包含以下元素
- Envelope 元素,用於標識 XML 文件為 SOAP 訊息
- Header 元素,包含頭部資訊
- Body 元素,包含呼叫和響應資訊
- Fault 元素,包含錯誤和狀態資訊
以上所有元素都在 SOAP envelope 的預設名稱空間中宣告
http://www.w3.org/2003/05/soap-envelope
SOAP 編碼和資料型別的預設名稱空間是
http://www.w3.org/2003/05/soap-encoding
語法規則
以下是一些重要的語法規則
- SOAP 訊息 **必須** 使用 XML 進行編碼
- SOAP 訊息 **必須** 使用 SOAP Envelope 名稱空間
- SOAP 訊息 **不得** 包含 DTD 引用
- SOAP 訊息 **不得** 包含 XML 處理指令
Skeleton SOAP 訊息
<?xml version="1.0"?>
<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 Envelope 元素
必需的 SOAP Envelope 元素是 SOAP 訊息的根元素。此元素將 XML 文件定義為 SOAP 訊息。
示例
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
...
訊息資訊在此處
...
</soap:Envelope>
xmlns:soap 名稱空間
請注意上面示例中的 xmlns:soap 名稱空間。它應始終具有值:"http://www.w3.org/2003/05/soap-envelope"。
該名稱空間將 Envelope 定義為 SOAP Envelope。
如果使用不同的名稱空間,應用程式將生成錯誤並丟棄該訊息。
encodingStyle 屬性
encodingStyle 屬性用於定義文件中使用的資料型別。此屬性可以出現在任何 SOAP 元素上,並且適用於該元素的以及所有子元素的名稱。
SOAP 訊息沒有預設編碼。
語法
soap:encodingStyle="URI"
示例
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
...
訊息資訊在此處
...
</soap:Envelope>
SOAP Header 元素
可選的 SOAP Header 元素包含關於 SOAP 訊息的應用程式特定資訊(如身份驗證、付款等)。
如果存在 Header 元素,它必須是 Envelope 元素的第一個子元素。
注意: Header 元素的所有直接子元素都必須是名稱空間限定的。
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://w3schools.tw/transaction/"
soap:mustUnderstand="1">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
上面的示例包含一個帶有 "Trans" 元素的頭部,一個值為 1 的 "mustUnderstand" 屬性,以及值為 234 的內容。
SOAP 在預設名稱空間中定義了三個屬性。這些屬性是:mustUnderstand、actor 和 encodingStyle。
在 SOAP Header 中定義的屬性決定了接收者應如何處理 SOAP 訊息。
mustUnderstand 屬性
SOAP mustUnderstand 屬性可用於指示頭部條目對接收者來說是必需的還是可選的。
如果將 mustUnderstand="1" 新增到 Header 元素的子元素中,則表示處理 Header 的接收者必須識別該元素。如果接收者不識別該元素,它在處理 Header 時會失敗。
語法
soap:mustUnderstand="0|1"
示例
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://w3schools.tw/transaction/"
soap:mustUnderstand="1">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
actor 屬性
SOAP 訊息可能從傳送方到接收方,在訊息路徑上傳遞不同的端點。但是,並非 SOAP 訊息的所有部分都可能針對最終端點,而是可能針對訊息路徑上一個或多個端點。
SOAP actor 屬性用於將 Header 元素指向特定的端點。
語法
soap:actor="URI"
示例
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://w3schools.tw/transaction/"
soap:actor="https://w3schools.tw/code/">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>
encodingStyle 屬性
encodingStyle 屬性用於定義文件中使用的資料型別。此屬性可以出現在任何 SOAP 元素上,並且適用於該元素的以及所有子元素的名稱。
SOAP 訊息沒有預設編碼。
語法
soap:encodingStyle="URI"
SOAP Body 元素
必需的 SOAP Body 元素包含傳送給訊息最終端點的實際 SOAP 訊息。
SOAP Body 元素的直接子元素可以是名稱空間限定的。
示例
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<m:GetPrice xmlns:m="https://w3schools.tw/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/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<m:GetPriceResponse xmlns:m="https://w3schools.tw/prices">
<m:Price>1.90</m:Price>
</m:GetPriceResponse>
</soap:Body>
</soap:Envelope>
SOAP Fault 元素
可選的 SOAP Fault 元素用於指示錯誤訊息。
SOAP Fault 元素包含 SOAP 訊息的錯誤和狀態資訊。
如果存在 Fault 元素,它必須作為 Body 元素的子元素出現。一個 Fault 元素在一個 SOAP 訊息中只能出現一次。
SOAP Fault 元素具有以下子元素
子元素 | 描述 |
---|---|
<faultcode> | 用於識別錯誤的代號 |
<faultstring> | 對錯誤的易於理解的解釋 |
<faultactor> | 關於誰導致了錯誤的發生的資訊 |
<detail> |
包含與 Body 元素相關的應用程式特定的錯誤資訊 |
SOAP 故障程式碼
在描述故障時,必須在 faultcode 元素中使用下面定義的 faultcode 值
Error | 描述 |
---|---|
VersionMismatch | 發現了 SOAP Envelope 元素的無效名稱空間 |
MustUnderstand | Header 元素的直接子元素,其 mustUnderstand 屬性設定為 "1",但未被理解 |
Client | 訊息格式不正確或包含不正確的資訊 |
Server | 伺服器出現問題,導致訊息無法繼續 |
HTTP 協議
HTTP 透過 TCP/IP 進行通訊。HTTP 客戶端使用 TCP 連線到 HTTP 伺服器。建立連線後,客戶端可以向伺服器傳送 HTTP 請求訊息
POST /item HTTP/1.1
Host: 189.123.255.239
Content-Type: text/plain
Content-Length: 200
然後伺服器處理請求,並將 HTTP 響應傳送回客戶端。響應包含一個狀態碼,指示請求的狀態
200 OK
Content-Type: text/plain
Content-Length: 200
在上面的示例中,伺服器返回了狀態碼 200。這是 HTTP 的標準成功程式碼。
如果伺服器無法解碼請求,它可能會返回類似這樣的內容
400 Bad Request
Content-Length: 0
SOAP 繫結
SOAP 規範定義了 SOAP 訊息的結構,而不是如何交換它們。這個空白由所謂的“SOAP 繫結”來填補。SOAP 繫結是允許使用傳輸協議有效交換 SOAP 訊息的機制。
大多數 SOAP 實現都提供了常見傳輸協議(如 HTTP 或 SMTP)的繫結。
HTTP 是同步的,並且被廣泛使用。SOAP HTTP 請求至少指定兩個 HTTP 頭部:Content-Type 和 Content-Length。
SMTP 是非同步的,並且用於最後手段或特定情況。
SOAP 的 Java 實現通常為 JMS (Java Messaging System) 協議提供特定的繫結。
Content-Type
SOAP 請求和響應的 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
SOAP 請求和響應的 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/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/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/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/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>