基於REST架構的Web Service設計

SOAP的Web Service解決方案雖然較爲成熟,且安全性較好,但是使用門檻較高,在大併發情況下會有性能問題,在互聯網上使用不太普及,因此並不太適合Web 2.0網站服務使用,目前大量的Web 2.0網站使用另外一種解決方案——REST。

  REST的架構設計

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

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

  REST架構讓人們真正理解我們的網絡協議HTTP本來面貌,對資源的操作包括獲取、創建、修改和刪除資源的操作正好對應HTTP協議提供的GET、POST、PUT和DELETE方法,因此REST把HTTP對一個URL資源的操作限制在GET、POST、PUT和DELETE這四個之內。這種針對網絡應用的設計和開發方式,可以降低開發的複雜性,提高系統的可伸縮性。

  REST的設計準則

  REST架構是針對Web應用而設計的,其目的是爲了降低開發的複雜性,提高系統的可伸縮性。REST提出瞭如下設計準則:

  網絡上的所有事物都被抽象爲資源(resource);

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

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

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

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

  使用REST架構

  對於開發人員來說,關心的是如何使用REST架構,這裏我們來簡單談談這個問題。REST不僅僅是一種嶄新的架構,它帶來的更是一種全新的Web開發過程中的思維方式:通過URL來設計系統結構。REST是一套簡單的設計原則、一種架構風格(或模式),不是一種具體的標準或架構。REST有很多成功的使用案例,著名的Delicious和Flickr都提供基於REST風格的API使用,客戶端調用也極其方便,下面是我用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")

  服務端代碼:

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服務器默認是不支持ASP文件的PUT和DELETE操作,默認會返回“403 - Forbidden”錯誤,因此需要修改IIS的設置,修改方法是:管理根據-IIS信息服務器-網站-屬性-主目錄-應用程序配置-配置-映射,選擇ASP - 編輯 - 修改爲全部動作。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章