原文出处:http://www.williamlong.info/archives/1728.html


-----------------------------------------------------------------------割----------------------------------------------------------------

---------------------


先前我曾经介绍过利用Apache Axis实现基于SOAP的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 - 编辑 - 修改为全部动作。


  关于更多关于REST方面的知识,建议阅读《RESTful Web Services》这本书。

最新文章

  1. 关于EventSource的精华
  2. sublime 安装插件GitGutter报错,git binary cannot be found等等
  3. sql语句错误
  4. 解决iphone填写表单时,表单项获取焦点时往下拉屏,导致顶部标题栏下滑错位
  5. lintcode 中等题:Majority number II 主元素 II
  6. 辛星浅析跨域传输的CORS解决方式
  7. 配置BeanUtils包,同时也是对导入第三包的步骤说明
  8. 控件篇:CheckedListBox的全选与反选
  9. H5学习之旅-H5的样式(5)
  10. 利用OpenCV给图像添加中文标注
  11. canutils上板测试问题记录
  12. 关于spark进行实时日志解析,保存hbase与mysql
  13. 使用delphi-cross-socket 开发kbmmw smart http service
  14. mac 安装 mysql.tar.gz
  15. VMware里Ubuntu-14.04-desktop的VMware Tools安装图文详解
  16. 公司内网静态IP,外网无线动态IP 同时上网,不必再切换网卡啦 route 命令给你搞定。
  17. 「小程序JAVA实战」小程序通用模板的使用(17)
  18. 201671010140. 2016-2017-2 《Java程序设计》java学习第十一周
  19. laravel框架容器管理的一些要点(转)
  20. java 对象与二进制互转

热门文章

  1. 关于.Net WebAPI数据认证(包括登陆认证、模型认证)
  2. js中 var functionName = function() {} 和 function functionName() {} 两种函数声明的区别 (译)
  3. MVC分页技术
  4. kafka存储机制以及offset
  5. carousel 插件隐藏列表中几项导致左右切换出错
  6. 微信小程序电商实战-入门篇
  7. Android 通过接口的方式去调用服务里面的方法
  8. Android 仿微信朋友圈拍小视频上传到服务器
  9. artTemplate教程
  10. Azure进阶攻略 | 该如何唤醒你?因内核超时而沉睡的Linux虚拟机!