REST学习总结

在.NET世界中,一提到Web Service首先想到的肯定是WSDL、SOAP等术语,但Web Service的实现方式不仅仅只有这一种,Web Service的实现方式包括:XML-RPC、REST和RPC+REST混合型。.NET中的Web Service属于XML-RPC风格。本文中谈到的REST将是另一种新奇的Web Service实现方式。

REST:表象化状态转变(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。其基本思想是通过HTTP协议提供的方法(GET、POST、PUT和DELETE)访问URI资源,服务器端通过识别URI和具体的HTTP来完成功能。REST风格的Web Service在系统交互中使用的是标准的HTTP内容,相对于XML-RPC使用SOAP对交互内容进行封装的做法显得更简洁。

REST示例:
假设原先有下面一个Web Service接口:
public class OrderService : System.Web.Services.WebService
{

    [WebMethod]
    [WebService(Description
="创建新订单")]
    
public string NewOrder(string productName, int productCount) 
    {
        
//
    }

    [WebMethod]
    [WebService(Description 
= "获取商户的所有订单")]
    
public IList<Order> GetOrders(string mercId)
    {
        
//
    }

    [WebMethod]
    [WebService(Description 
= "获取订单的详细信息")]
    
public Order GetOrder(string orderId)
    {
        
//
    }
    
}

使用REST风格来实现同样的接口,就需要:
1. 定义URI
http://example.com/order
http://example.com/order/1234
http://example.com/orders/merc12345
2. 通过HttpHander统一处理请求
3. 客户端模拟HTTP请求来调用接口
3.1 获取订单"1234"的详细信息
GET /order/1234 HTTP/1.1
Host: example.com
3.2 获取商户"merc12345"的所有订单
GET /orders/merc12345 HTTP/1.1
Host: example.com 
3.3 创建新订单
POST /order HTTP/1.1
Host: example.com 
productName=xxx
&productCount=10



参考文章:
《深入浅出REST》 http://www.infoq.com/cn/articles/rest-introduction
《Make Yahoo! Web Service REST Calls With C#》http://developer.yahoo.com/dotnet/howto-rest_cs.html
posted @ 2009-08-27 11:50  蛤蟆  阅读(713)  评论(1编辑  收藏  举报