- 相關(guān)推薦
Web Service的開(kāi)發(fā)與應用基礎
Web Service基于SOAP協(xié)議,而SOAP本身符合XML語(yǔ)法規范。雖然.NET為Web Service提供了強大的支持,但了解其基本機制對于程序員來(lái)說(shuō)仍然是必需的。
1.1 神馬是SOAP協(xié)議?
SOAP協(xié)議的全稱(chēng)是簡(jiǎn)單對象訪(fǎng)問(wèn)協(xié)議(Simple Object Access Protocol),SOAP致力于以XML形式提供一個(gè)簡(jiǎn)單、輕量的用于在分散或分布環(huán)境中交換結構化和類(lèi)型信息的機制。SOAP只規范對象訪(fǎng)問(wèn)的方式,而不限制具體實(shí)現的技術(shù)環(huán)境,這意味著(zhù)SOAP協(xié)議是一種跨平臺的協(xié)議:一個(gè).NET客戶(hù)端程序可以按照SOAP協(xié)議訪(fǎng)問(wèn)一個(gè)基于JavaEE技術(shù)體系結構的Web Service。SOAP訪(fǎng)問(wèn)仍然基于HTTP協(xié)議,同時(shí)其內容又以XML形式展現。
SOAP規范由四部分組成:
、 SOAP信封(SOAP envelop)
、 SOAP編碼規則(SOAP encoding rules)
、 SOAP RPC表示(SOAP RPC representation)
、 SOAP綁定(SOAP binding)
這里不對這四部分展開(kāi)介紹,通過(guò)下面的一個(gè)小例子來(lái)直觀(guān)地認識一下。
。1)在Web服務(wù)端,打算對外提供一個(gè)公共方法來(lái)供客戶(hù)端調用,而客戶(hù)端則需要提供這個(gè)方法需要的參數,并且最終得到返回值。假設這個(gè)方法被申明在MySimpleService.asmx文件中:
[WebMethod]
public string GetSumString(int para1, int para2)
{
int result = para1 + para2;
return result.ToString();
}
。2)當客戶(hù)端試圖使用這個(gè)Web Service方法時(shí),就需要向服務(wù)器端發(fā)出這樣的一個(gè)HTTP請求:
POST /MySimpleService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetSumString"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetSumString xmlns="http://tempuri.org/">
<para1>250</para1>
<para2>250</para2>
</GetSumString>
</soap:Body>
</soap:Envelope>
。3)等到Web Service服務(wù)器端接收到上面的請求之后,就可以進(jìn)行相應的邏輯處理,并且返回結果。根據SOAP協(xié)議,HTTP響應如下形式:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetSumStringResponse xmlns="http://tempuri.org/">
<GetSumStringResult>500</GetSumStringResult>
</GetSumStringResponse>
</soap:Body>
</soap:Envelope>
如此一來(lái),客戶(hù)端就得到了服務(wù)端的處理結果,換句話(huà)說(shuō),客戶(hù)端已經(jīng)得到了Web Service提供的服務(wù)。
PS:最后,再說(shuō)一下SOAP協(xié)議和HTTP協(xié)議,它們的關(guān)系非常類(lèi)似于網(wǎng)絡(luò )分層中的上下層協(xié)議,使用SOAP協(xié)議的雙方將SOAP數據包放入HTTP報文之中,并且通過(guò)HTTP協(xié)議完成實(shí)際的傳輸,換句話(huà)說(shuō),SOAP是對HTTP的一個(gè)封裝,下圖說(shuō)明了這一過(guò)程:
1.2 WSDL又是什么鬼,它有啥作用?
。1)WSDL介紹
WSDL(Web Service Description Language)是Web服務(wù)描述語(yǔ)言,它是一種由微軟、IBM、Intel等大型供應商提出的語(yǔ)言規范,目的就是為了描述Web服務(wù)器所提供的服務(wù),以供使用者參考。WSDL是一種復合XML語(yǔ)法規范的語(yǔ)言,它的設計完全基于SOAP協(xié)議,當一個(gè)Web Service服務(wù)器期望為使用者提供服務(wù)說(shuō)明時(shí),WSDL是最好的選擇之一。
這里仍以上面的實(shí)例來(lái)說(shuō)明,在Web服務(wù)端提供了這樣一個(gè)方法:
string GetSumString(int para1, int para2)
當服務(wù)端視圖利用WSDL告訴客戶(hù)端如何使用該方法時(shí),就會(huì )提供下面的這樣一個(gè)WSDL文件(仍然是一個(gè)XML):
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="GetSumString">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="para1" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="para2" type="s:int" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetSumStringResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetSumStringResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="GetSumStringSoapIn">
<wsdl:part name="parameters" element="tns:GetSumString" />
</wsdl:message>
<wsdl:message name="GetSumStringSoapOut">
<wsdl:part name="parameters" element="tns:GetSumStringResponse" />
</wsdl:message>
<!-- 這里省略其他定義 -->
</wsdl:definitions>
如上x(chóng)ml所示,在<wsdl:types>節點(diǎn)下,WSDL定義了GetSumString方法的名字:
<s:element name="GetSumString">
參數數量、每個(gè)參數的類(lèi)型:
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="para1" type="s:int" />
<s:element minOccurs="1" maxOccurs="1" name="para2" type="s:int" />
</s:sequence>
</s:complexType>
以及返回參數的類(lèi)型:
<s:element name="GetSumStringResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetSumStringResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
通過(guò)完整的描述,使用者就能夠了解如何使用該Web服務(wù)了。
。2)獲取和使用WSDL
當Web Service服務(wù)器提供WSDL時(shí),就可以通過(guò)特定的工具獲得WSDL文件。最直接的方式就是在URL中直接添加WSDL參數,來(lái)發(fā)送得到WSDL文件的請求,如下所示:
http://localhost:6105/MySimpleService.asmx?wsdl
這時(shí)點(diǎn)擊回車(chē)就可以得到如下圖所示的WSDL結果:
1.3 Web Service中如何處理附件?
盡管Web Service提供的方法的參數類(lèi)型沒(méi)有任何限制,也就意味著(zhù)所有的附件可以通過(guò)字節數組來(lái)進(jìn)行傳遞,但是把字節流直接內嵌在SOAP消息的做法有很多問(wèn)題,這也曾經(jīng)成為XML語(yǔ)法和SOAP協(xié)議被詬病的原因。這里主要介紹一下XOP的概念。
在XOP出現之前,SOAP處理二進(jìn)制數據的方式都很簡(jiǎn)單,比如當一個(gè)Web Service服務(wù)端提供了如下的方法時(shí):
void UploadSmallAttach(Byte[] attachment)
客戶(hù)端調用該Web Service,只需要發(fā)出下面這樣的SOAP請求即可:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<UploadSmallAttach xmlns="http://tempuri.org/">
<attachment>D0CF11E0A1B11AE100000000000000000000000003E00003
00FEFF09000600000000000000000000000600000000000000000000
DE0200000000000000000000001000000000000000FEFFFFFFFF000
00000000000000000D80200000000000D9020000DA02000DB02000
000DC020000DD0200000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFF</attachment>
</UploadSmallAttach>
</soap:Body>
</soap:Envelope>
如上所示,其中<attachment>節點(diǎn)下的一大堆字符,就是某個(gè)文件的字節流。通過(guò)這種方式,確實(shí)是可以實(shí)現傳送二進(jìn)制附件的功能的,但這樣的處理過(guò)于粗略,且傳輸沒(méi)有任何優(yōu)化。W3C為此特別指定了XOP規范。
XOP(XML-binary Optimized Packages)意為XML二進(jìn)制打包,它把二進(jìn)制數據流從SOAP消息中分離出來(lái),進(jìn)行單獨打包。上述的客戶(hù)端請求如果使用XOP規范的話(huà),將轉變?yōu)槿缦陆Y果:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<UploadSmallAttach xmlns="http://tempuri.org/">
<attachment>
<xop:Include xmlns="http://www.w3.org/2015/10/02/xop/include" href="cid:http://www.book.com/attachment.png" />
</attachment>
</UploadSmallAttach>
</soap:Body>
</soap:Envelope>
可以看到,原本出現在二進(jìn)制字節流的地方,被轉換成了一個(gè)引用:
<attachment>
<xop:Include xmlns="http://www.w3.org/2015/10/02/xop/include" href="cid:http://www.book.com/attachment.png" />
</attachment>
這樣整個(gè)SOAP信封節點(diǎn)下就不再包含任何二進(jìn)制直接,而福建則被安放在另一個(gè)MIME體中:
Content-Type: image/png
Content-Transfer-Encoding: binary
Content-ID: <sample@book.com>
D0CF11E0A1B11AE100000000000000000000000003E0000300FEFF090006
00000000000000000000000600000000000000000000DE0200000000000
000000000001000000000000000FEFFFFFFFF00000000000000000000D8
0200000000000D9020000DA02000DB02000000DC020000DD020000000
0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
【W(wǎng)eb Service的開(kāi)發(fā)與應用基礎】相關(guān)文章:
Web Workers加速移動(dòng)Web應用03-30
web瀏覽創(chuàng )作效果精選03-29
Web 2.0技術(shù)的內容03-30
Web開(kāi)發(fā)的教程圖解03-30
web標準常見(jiàn)問(wèn)題03-30
WEB教程標準應用標簽03-30
基于web的綜合測評與分析03-30
集成spring與Web容器教程03-20
關(guān)于java-web的試卷03-30