<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>

            Web Service的開發與應用基礎

            時間:2024-07-12 13:13:55 Web Services 我要投稿
            • 相關推薦

            Web Service的開發與應用基礎

              Web Service基于SOAP協議,而SOAP本身符合XML語法規范。雖然.NET為Web Service提供了強大的支持,但了解其基本機制對于程序員來說仍然是必需的。

              1.1 神馬是SOAP協議?

              SOAP協議的全稱是簡單對象訪問協議(Simple Object Access Protocol),SOAP致力于以XML形式提供一個簡單、輕量的用于在分散或分布環境中交換結構化和類型信息的機制。SOAP只規范對象訪問的方式,而不限制具體實現的技術環境,這意味著SOAP協議是一種跨平臺的協議:一個.NET客戶端程序可以按照SOAP協議訪問一個基于JavaEE技術體系結構的Web Service。SOAP訪問仍然基于HTTP協議,同時其內容又以XML形式展現。

              SOAP規范由四部分組成:

              ① SOAP信封(SOAP envelop)

              ② SOAP編碼規則(SOAP encoding rules)

              ③ SOAP RPC表示(SOAP RPC representation)

              ④ SOAP綁定(SOAP binding)

              這里不對這四部分展開介紹,通過下面的一個小例子來直觀地認識一下。

              (1)在Web服務端,打算對外提供一個公共方法來供客戶端調用,而客戶端則需要提供這個方法需要的參數,并且最終得到返回值。假設這個方法被申明在MySimpleService.asmx文件中:

              [WebMethod]

              public string GetSumString(int para1, int para2)

              {

              int result = para1 + para2;

              return result.ToString();

              }

              (2)當客戶端試圖使用這個Web Service方法時,就需要向服務器端發出這樣的一個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服務器端接收到上面的請求之后,就可以進行相應的邏輯處理,并且返回結果。根據SOAP協議,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>

              如此一來,客戶端就得到了服務端的處理結果,換句話說,客戶端已經得到了Web Service提供的服務。

              PS:最后,再說一下SOAP協議和HTTP協議,它們的關系非常類似于網絡分層中的上下層協議,使用SOAP協議的雙方將SOAP數據包放入HTTP報文之中,并且通過HTTP協議完成實際的傳輸,換句話說,SOAP是對HTTP的一個封裝,下圖說明了這一過程:

              1.2 WSDL又是什么鬼,它有啥作用?

              (1)WSDL介紹

              WSDL(Web Service Description Language)是Web服務描述語言,它是一種由微軟、IBM、Intel等大型供應商提出的語言規范,目的就是為了描述Web服務器所提供的服務,以供使用者參考。WSDL是一種復合XML語法規范的語言,它的設計完全基于SOAP協議,當一個Web Service服務器期望為使用者提供服務說明時,WSDL是最好的選擇之一。

              這里仍以上面的實例來說明,在Web服務端提供了這樣一個方法:

              string GetSumString(int para1, int para2)

              當服務端視圖利用WSDL告訴客戶端如何使用該方法時,就會提供下面的這樣一個WSDL文件(仍然是一個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>

              如上xml所示,在<wsdl:types>節點下,WSDL定義了GetSumString方法的名字:

              <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 name="GetSumStringResponse">

              <s:complexType>

              <s:sequence>

              <s:element minOccurs="0" maxOccurs="1" name="GetSumStringResult" type="s:string" />

              </s:sequence>

              </s:complexType>

              </s:element>

              通過完整的描述,使用者就能夠了解如何使用該Web服務了。

              (2)獲取和使用WSDL

              當Web Service服務器提供WSDL時,就可以通過特定的工具獲得WSDL文件。最直接的方式就是在URL中直接添加WSDL參數,來發送得到WSDL文件的請求,如下所示:

              http://localhost:6105/MySimpleService.asmx?wsdl

              這時點擊回車就可以得到如下圖所示的WSDL結果:

              1.3 Web Service中如何處理附件?

              盡管Web Service提供的方法的參數類型沒有任何限制,也就意味著所有的附件可以通過字節數組來進行傳遞,但是把字節流直接內嵌在SOAP消息的做法有很多問題,這也曾經成為XML語法和SOAP協議被詬病的原因。這里主要介紹一下XOP的概念。

              在XOP出現之前,SOAP處理二進制數據的方式都很簡單,比如當一個Web Service服務端提供了如下的方法時:

              void UploadSmallAttach(Byte[] attachment)

              客戶端調用該Web Service,只需要發出下面這樣的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>節點下的一大堆字符,就是某個文件的字節流。通過這種方式,確實是可以實現傳送二進制附件的功能的,但這樣的處理過于粗略,且傳輸沒有任何優化。W3C為此特別指定了XOP規范。

              XOP(XML-binary Optimized Packages)意為XML二進制打包,它把二進制數據流從SOAP消息中分離出來,進行單獨打包。上述的客戶端請求如果使用XOP規范的話,將轉變為如下結果:

              <?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>

              可以看到,原本出現在二進制字節流的地方,被轉換成了一個引用:

              <attachment>

              <xop:Include xmlns="http://www.w3.org/2015/10/02/xop/include" href="cid:http://www.book.com/attachment.png" />

              </attachment>

              這樣整個SOAP信封節點下就不再包含任何二進制直接,而福建則被安放在另一個MIME體中:

              Content-Type: image/png

              Content-Transfer-Encoding: binary

              Content-ID: <sample@book.com>
              D0CF11E0A1B11AE100000000000000000000000003E0000300FEFF090006
            00000000000000000000000600000000000000000000DE0200000000000
            000000000001000000000000000FEFFFFFFFF00000000000000000000D8
            0200000000000D9020000DA02000DB02000000DC020000DD020000000
            0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

            【Web Service的開發與應用基礎】相關文章:

            Web Workers加速移動Web應用03-30

            web瀏覽創作效果精選03-29

            Web 2.0技術的內容03-30

            Web開發的教程圖解03-30

            web標準常見問題03-30

            WEB教程標準應用標簽03-30

            基于web的綜合測評與分析03-30

            集成spring與Web容器教程03-20

            關于java-web的試卷03-30

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看