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服務。使用向導,完成如下的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 |
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 )]
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消息格式。
![clipboard[26] clipboard[26]](http://img1.51cto.com/attachment/201402/7/2359144_1391750081K061.png)
注意:在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消息格式。生成并補充數據,得到如下的格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
< soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem = "http://tempuri.org/" >
< soapenv:Header />
< soapenv:Body >
< tem:ChangeCarUnit >
< tem:s >
< 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實現。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 |
<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的調用。