Rest Api(转载)

来源:http://www.cnblogs.com/springyangwc/archive/2012/01/18/2325784.html

概述

REST 从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表示方式。获得这些表徵致使这些应用程序转变了其状态。随着不断获取资源的表示方式,客户端应用不断地在转变着其状态,所谓表述性状态转移(Representational State Transfer)。

这一观点不是凭空臆造的,而是通过观察当前Web互联网的运作方式而抽象出来的。Roy Fielding 认为,

“设计良好的网络应用表现为一系列的网页,这些网页可以看作的虚拟的状态机,用户选择这些链接导致下一网页传输到用户端展现给使用的人,而这正代表了状态的转变。”

REST是设计风格而不是标准。REST通常基于使用HTTP,URI,和XML以及HTML这些现有的广泛流行的协议和标准。

  • 资源是由URI来指定。
  • 对资源的操作包括获取、创建、修改和删除资源,这些操作正好对应HTTP协议提供的GET、POST、PUT和DELETE方法。
  • 通过操作资源的表现形式来操作资源。
  • 资源的表现形式则是XML或者HTML,取决于读者是机器还是人,是消费web服务的客户软件还是web浏览器。当然也可以是任何其他的格式。
REST的要求
  • 客户端和服务器结构
  • 连接协议具有无状态性
  • 能够利用Cache机制增进性能
  • 层次化的系统
  • 隨需代碼 - Javascript (可選)

 

RESTful Web 服务

RESTful Web 服务(也称为 RESTful Web API)是一个使用HTTP并遵循REST原则的Web服务。它从以下三个方面资源进行定义:URI,比如:http://example.com/resources/。

§ Web服务接受与返回的互联网媒体类型,比如:JSON,XML ,YAML 等。

§ Web服务在该资源上所支持的一系列请求方法(比如:POST,GET,PUT或DELETE)。

该表列出了在实现RESTful Web 服务时HTTP请求方法的典型用途。

HTTP 请求方法在RESTful Web 服务中的典型应用

 

资源

GET

PUT

POST

DELETE

一组资源的URI,比如http://example.com/resources/

列出 URI,以及该资源组中每个资源的详细信息(后者可选)。

使用给定的一组资源替换当前整组资源。

在本组资源中创建/追加一个新的资源。 该操作往往返回新资源的URL。

删除 整组资源。

单个资源的URI,比如http://example.com/resources/142

获取 指定的资源的详细信息,格式可以自选一个合适的网络媒体类型(比如:XML、JSON等)

替换/创建 指定的资源。并将其追加到相应的资源组中。

把指定的资源当做一个资源组,并在其下创建/追加一个新的元素,使其隶属于当前资源。

删除 指定的元素。

 

PUT 和 DELETE 方法是幂等方法。GET方法是安全方法 (不会对服务器端有修改,因此也是幂等的)。

不像基于SOAP的Web服务,RESTful Web服务并没有的“正式”标准。 这是因为REST是一种架构,而SOAP只是一个协议。虽然REST不是一个标准,但在实现RESTful Web服务时可以使用其他各种标准(比如HTTP,URL,XML,PNG等)。

REST的优点

  • 可以利用缓存Cache来提高响应速度
  • 通讯本身的无状态性可以让不同的服务器的处理一系列请求中的不同请求,提高服务器的扩展性
  • 浏览器即可作为客户端,简化软件需求
  • 相对于其他叠加在HTTP协议之上的机制,REST的软件依赖性更小
  • 不需要额外的资源发现机制
  • 在软件技术演进中的长期的兼容性更好

 

Rest 开发

首先先定义接口IRestHandler:

 1 /// <summary>
 2 /// The IRestHandler is an interface which provides Delete,Get,Post and Put methods.
 3 /// </summary>
 4 public interface IRestHandler : ICloneable
 5 {
 6     /// <summary>
 7     /// Delete method for RestHandler
 8     /// </summary>
 9     /// <param name="processor">The rest processor.</param>
10     /// <param name="authenticated">if set to <c>true</c> [authenticated].</param>
11     /// <returns>The http response</returns>
12     RestHandlerResponse Delete(IRestProcessor processor, bool authenticated);
13  
14     /// <summary>
15     /// Get method for RestHandler
16     /// </summary>
17     /// <param name="processor">The rest processor.</param>
18     /// <param name="authenticated">if set to <c>true</c> [authenticated].</param>
19     /// <returns>The http response</returns>
20     RestHandlerResponse Get(IRestProcessor processor, bool authenticated);
21  
22     /// <summary>
23     /// Post method for RestHandler
24     /// </summary>
25     /// <param name="processor">The rest processor.</param>
26     /// <param name="authenticated">if set to <c>true</c> [authenticated].</param>
27     /// <returns>The http response</returns>
28     RestHandlerResponse Post(IRestProcessor processor, bool authenticated);
29  
30     /// <summary>
31     /// Put method for RestHandler
32     /// </summary>
33     /// <param name="processor">The rest processor.</param>
34     /// <param name="authenticated">if set to <c>true</c> [authenticated].</param>
35     /// <returns>The http response</returns>
36     RestHandlerResponse Put(IRestProcessor processor, bool authenticated);
37 }

我们要定义一个HttpListener,先定义一个接口IRestListener:

 1 /// <summary>
 2 /// Listen an ip point and accept connection.
 3 /// </summary>
 4 public interface IRestListener : IDisposable
 5 {
 6     /// <summary>
 7     /// Gets or sets the max allowed connections to this listener.
 8     /// </summary>
 9     int MaxConnections { get; set; }
10  
11     /// <summary>
12     /// Gets or sets desktop rest manager.
13     /// </summary>
14     DesktopRestManager DesktopRestManager { get; set; }
15  
16     /// <summary>
17     /// Gets a value that indicate if it is listening.
18     /// </summary>
19     bool IsRunning { get; }
20  
21     /// <summary>
22     /// Gets or sets the server address information.
23     /// </summary>
24     IPEndPoint ServerAddress { get; set; }
25  
26     string Protocol { get; set; }
27  
28     /// <summary>
29     /// Start a listener.
30     /// </summary>
31     /// <returns>The ip end point to listen.</returns>
32     bool Start(TcpListener listener, IPEndPoint address);
33  
34     /// <summary>
35     /// Stop the listener.
36     /// </summary>
37     /// <returns>True if successfully, else false.</returns>
38     bool Stop();
39 }

接下来实现

public class HttpListener : IRestListener
    {
        public HttpListener(DesktopRestManager drm)
        {
            this.desktopRestManager = drm;
            this.isRunning = false;           
            MaxConnections = 50;
            this.serverAddress = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 10000);
            this.Protocol = "Http";           
        }
 
        #region IRestServer Members
 
        //public event ServerStatusChangedHandler ServerStatusChanged;
 
        public DesktopRestManager DesktopRestManager
        {
            get { return this.desktopRestManager; }
            set { this.desktopRestManager = value; }
        }
 
        public bool IsRunning
        {
            get { return this.isRunning; }
        }
 
        public IPEndPoint ServerAddress
        {
            get { return this.serverAddress; }
            set { this.serverAddress = value; }
        }
 
        public int MaxConnections
        {
            get;
            set;
        }
 
        public string Protocol
        {
            get;
            set;
        }
 
 
        public bool Start(TcpListener tcpListener, IPEndPoint address)
        {
            this.ServerAddress = address;
            this.listener = tcpListener;
            this.isRunning = true;
            Thread th = new Thread(new ThreadStart(this.Listening));
            th.Start();
            return true;
        }
 
        public bool Stop()
        {
            bool success = true; ;
            if (this.isRunning == true)
            {
                try
                {
                    this.isRunning = false;
                    if (listener != null)
                    {
                        listener.Stop();
                    }
                }
                catch (SocketException socketEx)
                {
                    _traceLog.InfoFormat("Stop http rest server: {0}", socketEx.Message);
                    success = false;
                }
                 
            }
            return success;
        }
 
        #endregion
 
        #region IDisposable Members
 
        public void Dispose()
        {
            this.Stop();
        }
 
        #endregion
 
        #region Private Methods
        private void Listening()
        {
            while (this.isRunning)
            {
                TcpClient tcpClient = null;
                try
                {
                    tcpClient = listener.AcceptTcpClient();
                    HttpConnection connection = new HttpConnection(tcpClient);
                    RestProcessor rh = new RestProcessor(this.desktopRestManager);
                    Thread processThread = new Thread(new ParameterizedThreadStart(req => connection.SendResponse(rh.HandleRequest(req as RestHandlerRequest))));
                    processThread.Name = "RestManager_Http_ProcessRequest";
                    processThread.Start(connection.GetRequest());
                }
                catch (SocketException socketEx)
                {
                    if (this.isRunning)
                    {
                        _traceLog.InfoFormat("Socket exception: {0}", socketEx.Message);
                    }
                    else
                    {
                        _traceLog.Info("The use stop the http listener.");
                    }
                    if (tcpClient != null && tcpClient.Connected)
                    {
                        tcpClient.Close();
                    }
                }
                catch (System.ArgumentNullException ex)
                {
                    _traceLog.ErrorFormat("Error occured: {0}", ex.Message);
                }
                catch (System.OutOfMemoryException ex)
                {
                    _traceLog.ErrorFormat("Error occured: {0}", ex.Message);
                }
                catch (System.Threading.ThreadStateException ex)
                {
                    _traceLog.ErrorFormat("Error occured: {0}", ex.Message);
                }
                catch (System.InvalidOperationException ex)
                {
                    _traceLog.ErrorFormat("Error occured: {0}", ex.Message);
                }
                catch (ApplicationException ex)
                {
                    _traceLog.ErrorFormat("Error occured: {0}", ex.Message);
                }
            }
            this.Stop();
        }
 
        #endregion
 
        #region Private Members
        private DesktopRestManager desktopRestManager;
        private bool isRunning;
        private IPEndPoint serverAddress;
        private TcpListener listener;
        private static LogManager _traceLog = new LogManager("RestManager-HttpListener");
        #endregion
    }

接下来处理HandleRequest:

/// <summary>
        /// Handles an http request for an Api call.
        /// </summary>
        public RestHandlerResponse HandleRequest(RestHandlerRequest rhr)
        {
            RestHandlerResponse res;
            // 50 Requests in maximum
            if (!this.restProcessorSemaphore.WaitOne(0))
            {
                 
               res = new RestHandlerResponse(503);
            }
 
            else
            {
                try
                {
                    // There is no need decode the url here, since the address will be decoded when it is parsed.
                    //rhr.Address = System.Web.HttpUtility.UrlDecode(rhr.Address);
 
                    res = this.process(rhr);
 
                }
                catch (RestManagerException ex)
                {
                    traceLog.ErrorFormat("Error happened while processing request\n{1}.\nException info:\n{0}  ",ex.Message);
                    res = new RestHandlerResponse(500);
                }
                try
                {
                    this.restProcessorSemaphore.Release();
                }
                catch (System.Threading.SemaphoreFullException)
                {
                    traceLog.ErrorFormat("Error happened while processing Semaphore.Release");
                }
                catch (System.IO.IOException)
                {
                    traceLog.ErrorFormat("Error happened while processing Semaphore.Release");
                }
                catch (System.UnauthorizedAccessException)
                {
                    traceLog.ErrorFormat("Error happened while processing Semaphore.Release");
                }
            }
 
            return res;
        }

接下来我们写发送请求代码:

private JObject MakeRequest(string url)
{
    var subsequentRequest = WebRequest.Create(url) as HttpWebRequest;
    subsequentRequest.Timeout = 30000;
    subsequentRequest.Headers.Add("Authorization", "OAuth " + TestToken);
    subsequentRequest.Headers.Add("App-User", TestUserName);
 
    WebResponse subsequentResponse;
 
    try
    {
        subsequentResponse = subsequentRequest.GetResponse();
        Stream stream = subsequentResponse.GetResponseStream();
        StreamReader sr = new StreamReader(stream);
        string output = sr.ReadToEnd();
        JObject jsonStr = JObject.Parse(output);
        return jsonStr;
 
    }
    catch (WebException ex)
    {
        if (ex.Response != null)
        {
            HttpWebResponse errorResponse = (HttpWebResponse)ex.Response;
            StreamReader reader = new StreamReader(errorResponse.GetResponseStream());
            string output = reader.ReadToEnd();
            JObject jsonStr = JObject.Parse(output);
            return jsonStr;
        }
        else
        {
            return null;
        }
    }
}

涉及项目的原因,代码只能提供这么多了,仅供参考

Json的返回结果格式如下:

[{"CreatedDate":"//Date(1299687080328+0800)//","Detail":"Do Something 1","Title":"Task1"},{"CreatedDate":"//Date(1299687080328+0800)//","Detail":"Do Something 5","Title":"Task5"}]

 

 

posted @ 2013-12-16 16:31  泉水叮~咚  阅读(385)  评论(0编辑  收藏  举报