標籤:
SOAP的概念應該不是什麼新鮮事物了。簡單的說,SOAP是以把資料以XML的方式組合起來,然後通過HTTP協議(也可以是其 它協議,但是通常總是用http協議)和遠程服務進行通訊的一個標準,也可以把它認為是一個中介軟體,起著溝通橋樑的作用。因為當所有的服務都使用同一種標 准,那麼溝通就比較容易了。
當然不得不承認,SOAP格式的報文內容冗餘,並且依賴於定義好的XML schemas,對於手工建立SOAP報文十分複雜,需要藉助一些工具來簡化工作。因此越來越多的Web服務傾向於使用Restful風格的WebService。
根據SOAP的協議,只需要發送有效SOAP訊息到服務端,就能實現通訊。那麼如何產生有效SOAP訊息?SOAP官方文檔詳細說明了SOAP的格式,但官方文檔很長,一般人沒耐心去讀完,大多時候僅僅在需要的時候去查一下,也可以去http://w3school.com.cn/soap/soap_syntax.asp學習一下簡化版的。這裡介紹一個工具,SoapUI,可以去它官網http://www.soapui.org/下載,它可以通過WSDL產生請求的SOAP訊息格式。
ASP.NET中,採用嚮導建立的Web服務,是SOAP風格的。假設我們建立一個web服務。使用嚮導,完成如下的代碼:
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;namespace WebService1{ /// <summary> /// Summary description for WebService1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public int Add(int a, int b) { return a + b; } /// <summary> /// 把公制單位的汽車轉化成以英制單位表示的汽車 /// </summary> /// <param name="s"></param> /// <returns></returns> [WebMethod] public car ChangeCarUnit(car s) { s.length = s.length * 3.3m; s.width = s.width * 3.3m; s.weight = s.width * 2.2m; return s; } } public class car { public string model = ""; public decimal length = 0; public decimal width = 0; public decimal weight = 0; }} |
方法都寫在WebService1.asmx檔案中,通過web服務的WSDL,可以得到它的SOAP訊息格式。這兩採用SoapUI輸入指定的WSDL檔案,即可以自動產生SOAP訊息格式。
注意:在ASP.net中,可以通過訪問WebService1.asmx並且輸入查詢字串?WSDL,即在IE瀏覽器中輸入WebService1.asmx?wsdl就可以獲得WSDL檔案。
還有一點要注意的是,微軟產生的WSDL檔案,有2個binding,分別表示soap1.1和soap1.2,它都支援。
因此在SoapUI中可以看到2個不同的WebService介面,其實是大同小異的。
雙擊Add的Request,就可以得到SOAP訊息格式了,其中的問號可以輸入指定的值,然後點擊執行按鈕,就又可以得到響應的SOAP訊息格式了。
通過SoapUI產生SOAP訊息後,就可以自己構造SOAP訊息來調用SOAP風格的WebService,因此,只要解決如何產生請求的SOAP訊息,我們甚至可以自己實現一個Web服務調用架構,無論是基於PHP,ASP.net,還是javascript。
-----------------------------------------------------------------------
下面的示範如何在javascript中發送SOAP。
一下代碼調用之前WebService1中的方法ChangeCarUnit,這個方法把汽車參數從公製為單位的轉換成英制單位。
首先手動通過SoapUI擷取SOAP訊息格式。產生並補充資料,得到如下的格式
| 123456789101112131415 |
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> <soapenv:Header/> <soapenv:Body> <tem:ChangeCarUnit> <!--Optional:--> <tem:s> <!--Optional:--> <tem:model>Passat</tem:model> <tem:length>4.87</tem:length> <tem:width>1.834</tem:width> <tem:weight>1435</tem:weight> </tem:s> </tem:ChangeCarUnit> </soapenv:Body></soapenv:Envelope> |
因此只需將這串xml發送到webservice既可。
通過jquery ajax實現。
代碼如下:
| 123456789101112131415161718192021222324252627282930313233 |
<script type="text/javascript"> $(function () { $("#btnclick").click(function () { var soapmessage = ""; soapmessage += ‘<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">‘; soapmessage += ‘<soapenv:Header/>‘; soapmessage += ‘<soapenv:Body>‘; soapmessage += ‘<tem:ChangeCarUnit>‘; soapmessage += ‘ <!--Optional:-->‘; soapmessage += ‘ <tem:s>‘; soapmessage += ‘ <!--Optional:-->‘; soapmessage += ‘ <tem:model>Passat</tem:model>‘; soapmessage += ‘ <tem:length>4.87</tem:length>‘; soapmessage += ‘ <tem:width>1.834</tem:width>‘; soapmessage += ‘ <tem:weight>1435</tem:weight>‘; soapmessage += ‘ </tem:s>‘; soapmessage += ‘</tem:ChangeCarUnit>‘; soapmessage += ‘ </soapenv:Body>‘; soapmessage += ‘</soapenv:Envelope>‘; var option = { url: ‘http://localhost:28689/WebService1.asmx‘, type: ‘POST‘, contentType: ‘text/xml‘, success: function (result) { alert(result.documentElement.textContent); }, data: soapmessage }; $.ajax(option); }); });</script><input value=‘click‘ type="button" id="btnclick" /> |
點擊按鈕後,就會將soap訊息post到web伺服器,並且得到返回訊息,返回訊息也是基於XML的文本。
通過上面的執行個體,我們可以通過編寫專用的工具來產生SOAP訊息,通過封裝後,再通過POST方式(比如c#中的HttpWebRequest)來實現webserverice的調用。
本文出自 “一隻部落格” 部落格,請務必保留此出處http://cnn237111.blog.51cto.com/2359144/1357029
(轉)基於SOAP的WebService的調用原理