何为SOAP
1.??? 什么是SOAP
SOAP全稱:簡單對象訪問協(xié)議(Simple Object Access Protocol),請區(qū)別與SOA的異同,SOA全稱是,面向服務(wù)體系結(jié)構(gòu)(Service-oriented architecture),別搞混淆。
SOAP是一種通信協(xié)議,它規(guī)定了一種以XML為信息載體進(jìn)行信息交互的協(xié)議,主要用于Web服務(wù)(Web Service)中,SOAP協(xié)議在使用時,可以將其綁定到HTTP協(xié)議,或TCP、UDP協(xié)議。
2.??? SOAP的特點(diǎn)
| SOAP 指簡易對象訪問協(xié)議 SOAP 是一種通信協(xié)議 SOAP 用于應(yīng)用程序之間的通信 SOAP 是一種用于發(fā)送消息的格式 SOAP 被設(shè)計用來通過因特網(wǎng)進(jìn)行通信 SOAP 獨(dú)立于平臺 SOAP 獨(dú)立于語言 SOAP 基于 XML SOAP 很簡單并可擴(kuò)展 SOAP 允許您繞過防火墻 SOAP 將被作為 W3C 標(biāo)準(zhǔn)來發(fā)展 |
?
3.??? SOAP的結(jié)構(gòu)及語法
3.1? SOAP消息的結(jié)構(gòu)
SOAP消息就是一個普通的xml文檔,SOAP格式的XML文檔主要有以下幾大元素:
| 必需的 Envelope 元素,可把此 XML 文檔標(biāo)識為一條 SOAP 消息 可選的 Header 元素,包含頭部信息 必需的 Body 元素,包含所有的調(diào)用和響應(yīng)信息 可選的 Fault 元素,提供有關(guān)在處理此消息所發(fā)生錯誤的信息 |
?
3.2? SOAP消息的語法規(guī)則
| SOAP 消息必須用 XML 來編碼 SOAP 消息必須使用 SOAP Envelope 命名空間 SOAP 消息必須使用 SOAP Encoding 命名空間 SOAP 消息不能包含 DTD 引用 SOAP 消息不能包含 XML 處理指令 |
?
3.3? SOAP消息結(jié)構(gòu)示例
| <?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屬性:
?? 作用:定義在文檔中使用的數(shù)據(jù)類型
?? 語法: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元素包含有關(guān) SOAP 消息的應(yīng)用程序?qū)S眯畔?#xff0c;如果使用Header元素,那么Header元素肯定是根元素Envelope的第一個子元素。
SOAP 在默認(rèn)的命名空間中 ("http://www.w3.org/2001/12/soap-envelope") 定義了三個屬性。這三個屬性是:actor、 mustUnderstand 以及 encodingStyle。這些被定義在 SOAP 頭部的屬性可定義容器如何對 SOAP 消息進(jìn)行處理。
所有 Header 元素的直接子元素必須是合格的命名空間。
actor屬性:
?? 作用:將 Header 元素尋址到一個特定的端點(diǎn)
?? 語法: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屬性:
?? 作用:用于標(biāo)識標(biāo)題項(xiàng)對于要對其進(jìn)行處理的接收者來說是強(qiáng)制的還是可選的。
?? 語法:soap:mustUnderstand="0|1",如果取1,則它可指示處理此頭部的接收者必須認(rèn)可此元素。假如此接收者無法認(rèn)可此元素,則在處理此頭部時必須失效。,
?? 示例:
| <?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 元素
包含打算傳送到消息最終端點(diǎn)的實(shí)際 SOAP 消息。
Body 元素的直接子元素可以是合格的命名空間
實(shí)例:
| <?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 元素是應(yīng)用程序?qū)S玫脑亍K鼈儾⒉皇?span lang="en-us"> SOAP 標(biāo)準(zhǔn)的一部分。
5.??? 將SOAP綁定到HTTP
其實(shí)SOAP就是:HTTP + XML = SOAP
傳統(tǒng)的訪問文本格式網(wǎng)頁的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 請求規(guī)定至少兩個 HTTP 頭:Content-Type 和 Content-Length。
?? 綁定語法:
n? Content-Type: MIMEType; charset=character-encoding
n? Content-Length: bytes
?? 綁定實(shí)例:
| POST /item HTTP/1.1 Content-Type: application/soap+xml; charset=utf-8 Content-Length: 250 |
?
6.??? SOAP實(shí)例
在下面的例子中,一個 GetStockPrice 請求被發(fā)送到了服務(wù)器。此請求有一個 StockName 參數(shù),而在響應(yīng)中則會返回一個 Price 參數(shù)。此功能的命名空間被定義在此地址中: "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 響應(yīng):
| 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> ? |
轉(zhuǎn)載于:https://blog.51cto.com/wanqiufeng/426779
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
- 上一篇: oracle 字符串转为正数用 to_n
- 下一篇: 内存的动态分区分配