何为SOAP
1.??? 什么是SOAP
SOAP全稱:簡單對象訪問協議(Simple Object Access Protocol),請區別與SOA的異同,SOA全稱是,面向服務體系結構(Service-oriented architecture),別搞混淆。
SOAP是一種通信協議,它規定了一種以XML為信息載體進行信息交互的協議,主要用于Web服務(Web Service)中,SOAP協議在使用時,可以將其綁定到HTTP協議,或TCP、UDP協議。
2.??? SOAP的特點
| SOAP 指簡易對象訪問協議 SOAP 是一種通信協議 SOAP 用于應用程序之間的通信 SOAP 是一種用于發送消息的格式 SOAP 被設計用來通過因特網進行通信 SOAP 獨立于平臺 SOAP 獨立于語言 SOAP 基于 XML SOAP 很簡單并可擴展 SOAP 允許您繞過防火墻 SOAP 將被作為 W3C 標準來發展 |
?
3.??? SOAP的結構及語法
3.1? SOAP消息的結構
SOAP消息就是一個普通的xml文檔,SOAP格式的XML文檔主要有以下幾大元素:
| 必需的 Envelope 元素,可把此 XML 文檔標識為一條 SOAP 消息 可選的 Header 元素,包含頭部信息 必需的 Body 元素,包含所有的調用和響應信息 可選的 Fault 元素,提供有關在處理此消息所發生錯誤的信息 |
?
3.2? SOAP消息的語法規則
| SOAP 消息必須用 XML 來編碼 SOAP 消息必須使用 SOAP Envelope 命名空間 SOAP 消息必須使用 SOAP Encoding 命名空間 SOAP 消息不能包含 DTD 引用 SOAP 消息不能包含 XML 處理指令 |
?
3.3? 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> |
4.??? SOAP元素講解
4.1? Envelope元素
是SOAP消息的根元素,且其命名空間始終是http://www.w3.org/2001/12/soap-envelope
如:
| <?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> |
?
Envelope元素包含的屬性有:encodingStyle??????
encodingStyle屬性:
?? 作用:定義在文檔中使用的數據類型
?? 語法: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> |
?
4.2? Header 元素
Header元素包含有關 SOAP 消息的應用程序專用信息,如果使用Header元素,那么Header元素肯定是根元素Envelope的第一個子元素。
SOAP 在默認的命名空間中 ("http://www.w3.org/2001/12/soap-envelope") 定義了三個屬性。這三個屬性是:actor、 mustUnderstand 以及 encodingStyle。這些被定義在 SOAP 頭部的屬性可定義容器如何對 SOAP 消息進行處理。
所有 Header 元素的直接子元素必須是合格的命名空間。
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="0|1",如果取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 屬性:
前面已介紹過
4.3? Body 元素
包含打算傳送到消息最終端點的實際 SOAP 消息。
Body 元素的直接子元素可以是合格的命名空間
實例:
| <?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:Body> ?? <m:GetPrice xmlns:m="http://www.w3school.com.cn/prices"> ????? <m:Item>Apples</m:Item> ?? </m:GetPrice> </soap:Body> ? </soap:Envelope> |
注:請注意,上面的 m:GetPrice 和 Item 元素是應用程序專用的元素。它們并不是 SOAP 標準的一部分。
5.??? 將SOAP綁定到HTTP
其實SOAP就是:HTTP + XML = SOAP
傳統的訪問文本格式網頁的HTTP頭是
| POST /item HTTP/1.1 Host: 189.123.345.239 Content-Type: text/plain Content-Length: 200 |
?
SOAP 請求可能是 HTTP POST 或 HTTP GET 請求。
HTTP POST 請求規定至少兩個 HTTP 頭:Content-Type 和 Content-Length。
?? 綁定語法:
n? Content-Type: MIMEType; charset=character-encoding
n? Content-Length: bytes
?? 綁定實例:
| POST /item HTTP/1.1 Content-Type: application/soap+xml; charset=utf-8 Content-Length: 250 |
?
6.??? 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/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/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/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/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> ? |
轉載于:https://blog.51cto.com/wanqiufeng/426779
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: oracle 字符串转为正数用 to_n
- 下一篇: 内存的动态分区分配