選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

XML 教程

XML 主頁 XML 簡介 XML 如何使用 XML 樹 XML 語法 XML 元素 XML 屬性 XML 名稱空間 XML 顯示 XML HttpRequest XML 解析器 XML DOM XML XPath XML XSLT XML XQuery XML XLink XML 驗證器 XML DTD XML Schema XML 伺服器 XML 示例 XML 測驗 XML 證書

XML AJAX

AJAX Introduction AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

XML DOM

DOM Introduction DOM Nodes DOM Accessing DOM Node Info DOM Node List DOM Traversing DOM Navigating DOM Get Values DOM Change Nodes DOM Remove Nodes DOM Replace Nodes DOM Create Nodes DOM Add Nodes DOM Clone Nodes DOM Examples

XPath 教程

XPath Introduction XPath Nodes XPath Syntax XPath Axes XPath Operators XPath Examples

XSLT 教程

XSLT Introduction XSL Languages XSLT Transform XSLT <template> XSLT <value-of> XSLT <for-each> XSLT <sort> XSLT <if> XSLT <choose> XSLT Apply XSLT on the Client XSLT on the Server XSLT Edit XML XSLT Examples

XQuery 教程

XQuery Introduction XQuery Example XQuery FLWOR XQuery HTML XQuery Terms XQuery Syntax XQuery Add XQuery Select XQuery Functions

XML DTD

DTD Introduction DTD Building Blocks DTD Elements DTD Attributes DTD Elements vs Attr DTD Entities DTD Examples

XSD Schema

XSD 簡介 XSD 如何做 XSD <schema> XSD 元素 XSD 屬性 XSD 限制 XSD 複雜元素 XSD 空 XSD 僅元素 XSD 僅文字 XSD 混合 XSD 指示符 XSD <any> XSD <anyAttribute> XSD 替換 XSD 示例

XSD Data Types

XSD String XSD Date/Time XSD Numeric XSD Misc XSD Reference

Web Services

XML Services XML WSDL XML SOAP XML RDF XML RSS

參考手冊

DOM 節點型別 DOM 節點 DOM NodeList DOM NamedNodeMap DOM Document DOM Element DOM Attribute DOM Text DOM CDATA DOM Comment DOM XMLHttpRequest DOM Parser XSLT 元素 XSLT/XPath 函式

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>

×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援