http://www.aygfsteel.com/ebecket 返還網(wǎng)
          隨筆-140  評論-11  文章-131  trackbacks-0
          先前我曾經(jīng)介紹過利用Apache Axis實現(xiàn)基于SOAP的Web Service實現(xiàn)技術(shù)和相關(guān)代碼,總的來說,SOAP的Web Service解決方案雖然較為成熟,且安全性較好,但是使用門檻較高,在大并發(fā)情況下會有性能問題,在互聯(lián)網(wǎng)上使用不太普及,因此并不太適合Web 2.0網(wǎng)站服務(wù)使用,目前大量的Web 2.0網(wǎng)站使用另外一種解決方案——REST。

            REST的架構(gòu)設(shè)計

            REST(Representational State Transfer)是一種輕量級的Web Service架構(gòu)風(fēng)格,其實現(xiàn)和操作明顯比SOAP和XML-RPC更為簡潔,可以完全通過HTTP協(xié)議實現(xiàn),還可以利用緩存Cache來提高響應(yīng)速度,性能、效率和易用性上都優(yōu)于SOAP協(xié)議。

            REST架構(gòu)遵循了CRUD原則,CRUD原則對于資源只需要四種行為:Create(創(chuàng)建)、Read(讀取)、Update(更新)和Delete(刪除)就可以完成對其操作和處理。這四個操作是一種原子操作,即一種無法再分的操作,通過它們可以構(gòu)造復(fù)雜的操作過程,正如數(shù)學(xué)上四則運算是數(shù)字的最基本的運算一樣。

            REST架構(gòu)讓人們真正理解我們的網(wǎng)絡(luò)協(xié)議HTTP本來面貌,對資源的操作包括獲取、創(chuàng)建、修改和刪除資源的操作正好對應(yīng)HTTP協(xié)議提供的GET、POST、PUT和DELETE方法,因此REST把HTTP對一個URL資源的操作限制在GET、POST、PUT和DELETE這四個之內(nèi)。這種針對網(wǎng)絡(luò)應(yīng)用的設(shè)計和開發(fā)方式,可以降低開發(fā)的復(fù)雜性,提高系統(tǒng)的可伸縮性。

            REST的設(shè)計準則

            REST架構(gòu)是針對Web應(yīng)用而設(shè)計的,其目的是為了降低開發(fā)的復(fù)雜性,提高系統(tǒng)的可伸縮性。REST提出了如下設(shè)計準則:

            網(wǎng)絡(luò)上的所有事物都被抽象為資源(resource);

            每個資源對應(yīng)一個唯一的資源標識符(resource identifier);

            通過通用的連接器接口(generic connector interface)對資源進行操作;

            對資源的各種操作不會改變資源標識符;

            所有的操作都是無狀態(tài)的(stateless)。

            使用REST架構(gòu)

            對于開發(fā)人員來說,關(guān)心的是如何使用REST架構(gòu),這里我們來簡單談?wù)勥@個問題。REST不僅僅是一種嶄新的架構(gòu),它帶來的更是一種全新的Web開發(fā)過程中的思維方式:通過URL來設(shè)計系統(tǒng)結(jié)構(gòu)。REST是一套簡單的設(shè)計原則、一種架構(gòu)風(fēng)格(或模式),不是一種具體的標準或架構(gòu)。REST有很多成功的使用案例,著名的Delicious和Flickr都提供基于REST風(fēng)格的API使用,客戶端調(diào)用也極其方便,下面是我用ASP寫的一個很簡單的REST舉例,從中可以看出REST是多么的簡單易用。

            客戶端代碼:

          Private Function httpGet(url, method, data)
              Dim xmlhttp
              Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
              xmlhttp.open method, url + "?" + data, False
              xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
              xmlhttp.setRequestHeader "Content-Length", Len(data)
              xmlhttp.send (Null)
              If (xmlhttp.Status = 200) Then httpGet = xmlhttp.responseText
              Set xmlhttp = Nothing
          End Function

          Private Function httpPost(url, method, data)
              Dim xmlhttp
              Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
              xmlhttp.open method, url, False
              xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
              xmlhttp.setRequestHeader "Content-Length", Len(data)
              xmlhttp.send (data)
              If (xmlhttp.Status = 200) Then httpPost = xmlhttp.responseText
              Set xmlhttp = Nothing
          End Function

          Private Function httpPut(url, method, data)
              Dim xmlhttp
              Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
              xmlhttp.open method, url, False
              xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
              xmlhttp.setRequestHeader "Content-Length", Len(data)
              xmlhttp.send (data)
              If xmlhttp.Status >= 400 And xmlhttp.Status <= 599 Then
                  response.write " Error Occurred : " & xmlhttp.Status & " - " & xmlhttp.statusText
              Else
                  response.write xmlhttp.responseText
              End If
              If (xmlhttp.Status = 200) Then httpPut = xmlhttp.responseText
              Set xmlhttp = Nothing
          End Function

          Private Function httpDelete(url, method, data)
              Dim xmlhttp
              Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
              xmlhttp.open method, url + "?" + data, False
              xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
              xmlhttp.setRequestHeader "Content-Length", Len(data)
              xmlhttp.send (Null)
              If xmlhttp.Status >= 400 And xmlhttp.Status <= 599 Then
                  response.write " Error Occurred : " & xmlhttp.Status & " - " & xmlhttp.statusText
              Else
                  response.write xmlhttp.responseText
              End If
              If (xmlhttp.Status = 200) Then httpDelete = xmlhttp.responseText
              Set xmlhttp = Nothing
          End Function

          response.write httpPost("http://localhost/rest/service.asp", "POST", "do=POST")
          response.write httpGet("http://localhost/rest/service.asp", "GET", "do=GET")
          response.write httpPut("http://localhost/rest/service.asp", "PUT", "do=PUT")
          response.write httpDelete("http://localhost/rest/service.asp", "DELETE", "do=DELETE")

            服務(wù)端代碼:

          Response.Write Request.ServerVariables("REQUEST_METHOD")
          If (Request.ServerVariables("REQUEST_METHOD")="GET") Then
           Response.Write "DO GET" + Request("do")
          ElseIf (Request.ServerVariables("REQUEST_METHOD")="POST") Then
           Response.Write "DO POST" + Request("do")
          ElseIf (Request.ServerVariables("REQUEST_METHOD")="PUT") Then
           Response.Write "DO PUT" + Request("do")
          ElseIf (Request.ServerVariables("REQUEST_METHOD")="DELETE") Then
           Response.Write "DO DELETE" + Request("do")
          End if

            需要注意的是,IIS服務(wù)器默認是不支持ASP文件的PUT和DELETE操作,默認會返回“403 - Forbidden”錯誤,因此需要修改IIS的設(shè)置,修改方法是:管理根據(jù)-IIS信息服務(wù)器-網(wǎng)站-屬性-主目錄-應(yīng)用程序配置-配置-映射,選擇ASP - 編輯 - 修改為全部動作。

            關(guān)于更多關(guān)于REST方面的知識,建議閱讀《RESTful Web Services》這本書。

          posted on 2009-11-11 09:48 becket_zheng 閱讀(255) 評論(0)  編輯  收藏 所屬分類: C#
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿

          隨筆分類(138)

          隨筆檔案(134)

          文章分類(77)

          文章檔案(109)

          email

          常去的Blog

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 内黄县| 财经| 邯郸县| 华坪县| 东丽区| 昂仁县| 马鞍山市| 连云港市| 喜德县| 瑞丽市| 墨脱县| 宕昌县| 靖边县| 玉龙| 达拉特旗| 赣州市| 济阳县| 嘉荫县| 西城区| 托克逊县| 疏附县| 安多县| 栖霞市| 彩票| 富源县| 太湖县| 隆德县| 桂阳县| 舟山市| 巴林右旗| 昆明市| 秦皇岛市| 咸丰县| 渭源县| 宁津县| 尼玛县| 大姚县| 彝良县| 普兰县| 喜德县| 蒙山县|