.NET RESTful Web Services入门

  很早之前看到过RESTful Web Services,并未在意,也没找相关资料进行学习。今天偶尔有一机会,就找了点资料进行研究,发现RESTful真是“简约而不简单”。下面用示例来说明:

1 项目结构

.NET RESTful Web Services入门

2 REST 服务接口定义

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace Jack_Restful_Service
{ [ServiceContract(Name = "RestfulService",Namespace="http://www.cnblogs.com/isaboy")]
public interface IRestDemoServices
{
[OperationContract]
[WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Bare)]
string GetClientNameById(string Id); [OperationContract]
[WebGet(UriTemplate = Routing.AddClientRoute, BodyStyle = WebMessageBodyStyle.Bare)]
string Add(string a, string b);
//error
//string Add(int a, int b); [OperationContract]
[WebGet(UriTemplate = Routing.LoginClientRoute, BodyStyle = WebMessageBodyStyle.Bare)]
string Login(string uname, string upwd); //post
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
Method = "POST", UriTemplate = "/Client/UpdateUser/{uname}")]
User UpdateUser(string uname, User newUser); }
//URI路由
public static class Routing
{
public const string GetClientRoute = "/Client/{id}"; public const string AddClientRoute = "/Client/{a},{b}";
//{uname}里面的参数名称要和string Login(string uname, string upwd);一致
public const string LoginClientRoute = "/Client/{uname}__{upwd}";
} }

3 REST服务接口实现

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
namespace Jack_Restful_Service
{ [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Single,
IncludeExceptionDetailInFaults = true,
Namespace = "http://www.cnblogs.com/isaboy")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestDemoServices : IRestDemoServices
{
//GET
public string GetClientNameById(string Id)
{
string ReturnString = "Your id is: " + Id; return ReturnString;
} public string Add(string a, string b)
{
int sum = int.Parse(a) + int.Parse(b);
return sum.ToString();
} public string Login(string uname, string upwd)
{
if (uname == "admin" && upwd == "admin")
{
return "success";
}
else
{
return "false";
}
}
//POST
public User UpdateUser(string uname, User newUser)
{
return newUser;
}
} }

4 将服务HOST

  Console.WriteLine("----------Restful Service Start--------------");
RestDemoServices demoServices = new RestDemoServices();
WebServiceHost _serviceHost = new WebServiceHost(demoServices, new Uri("http://localhost:8000/RestfulService"));
_serviceHost.Open();
Console.WriteLine("----------Restful Service Opened--------------");
Console.WriteLine("http://localhost:8000/RestfulService/Client/8");
Console.WriteLine("http://localhost:8000/RestfulService/Client/2,5");
Console.WriteLine("http://localhost:8000/RestfulService/Client/admin__admin");

5 打开浏览器,即可进行资源访问

.NET RESTful Web Services入门

另外,我们可以用代码进行测试

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.IO;
namespace PostServiceTest
{
class Program
{
static void Main(string[] args)
{
//get
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8000/RestfulService/Client/8");
WebResponse response = request.GetResponse();
string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.WriteLine(result); //post
string requestData = "{\"uname\":\"admin\",\"upwd\":\"admin\"}";
byte[] data = Encoding.UTF8.GetBytes(requestData);
request = (HttpWebRequest)WebRequest.Create("http://localhost:8000/RestfulService/Client/UpdateUser/admin");
request.Method = "POST";
request.ContentType = "application/json";
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, , data.Length);
dataStream.Close(); response = request.GetResponse();
result = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.WriteLine(result);
Console.ReadKey();
}
}
}

.NET RESTful Web Services入门

上一篇:xcode的调试技巧


下一篇:Jersey the RESTful Web Services in Java