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

1 项目结构

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 打开浏览器,即可进行资源访问

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

 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入门的更多相关文章

  1. RESTful Web Services初探

    RESTful Web Services初探 作者:杜刚 近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTf ...

  2. Jersey the RESTful Web Services in Java

    Jersey 是一个JAX-RS的实现, JAX-RS即Java API for RESTful Web Services, 支持按照表述性状态转移(REST)架构风格创建Web服务. REST 中最 ...

  3. 使用 Spring 3 来创建 RESTful Web Services

    来源于:https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/ 在 Java™ 中,您可以使用以下几种方法来创建 RESTful We ...

  4. 就是这么简单!使用Rest-assured 测试Restful Web Services

    使用 Rest-assured 测试 Restful Web Services 转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html 这里向大家介 ...

  5. cxf开发Restful Web Services

    一.restful web services rest全称是Representation State Transfer(表述性状态转移).它是一种软件架构风格,只是提供了一组设计原则和约束条件.在re ...

  6. RESTful Web Services测试工具推荐

    命令行控的最爱:cURL cURL是一个很强大的支持各种协议的文件传输工具,用它来进行RESTful Web Services的测试简直是小菜一碟.这个工具基本上类Unix操作系统(各种Linux.M ...

  7. 【转】RESTful Web Services初探

    近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTful风格的Web Services,比较著名的包括Twit ...

  8. 使用 Spring 3 来创建 RESTful Web Services(转)

    使用 Spring 3 来创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参 ...

  9. 基于Spring设计并实现RESTful Web Services(转)

    基于Spring设计并实现RESTful Web Services 在本教程中,你将会使用Spring来创建一个具有生产力的RESTful网络服务. 为什么用RESTful网络服务? 从和Amazon ...

随机推荐

  1. 分享两个BPM配置小技巧

    1.小技巧 流程图修改后发布的话版本号会+1,修改次数多了之后可能会导致版本号很高,这个时候可以将流程导出,然后删除对应的流程包再导入,发布数据模型和流程图之后,版本清零 2.小技巧 有的同事入职后使 ...

  2. npm源切换

    版权声明:欢迎转载,请附加转载来源:一路博客(http://www.16boke.com)   目录(?)[+] 安装 使用 列出可选的源 切换 增加源 删除源 测试速度 许可 项目主页   我们介绍 ...

  3. 在UPDATE中更新TOP条数据以及UPDATE更新中使用ORDER BY

    正常查询语句中TOP的运用: SELECT TOP 1000 * FROM MP_MemberGrade   随意更新一张表中满足条件的前N条数据: UPDATE TOP (1) MP_Member ...

  4. 《Note --- Unreal 4 --- Sample analyze --- StrategyGame(continue...)》

    ---------------------------------------------------------------------------------------------------- ...

  5. Spring5:@Autowired注解、@Resource注解和@Service注解

    什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...

  6. Google Java编程库Guava介绍

    本系列想介绍下Java下开源的优秀编程库--Guava[ˈgwɑːvə].它包含了Google在Java项目中使用一些核心库,包含集合(Collections),缓存(Caching),并发编程库(C ...

  7. ABP源码分析六:依赖注入的实现

    ABP的依赖注入的实现有一个本质两个途径:1.本质上是依赖于Castle这个老牌依赖注入的框架.2.一种实现途径是通过实现IConventionalDependencyRegistrar的实例定义注入 ...

  8. Spring Bean详细讲解

    什么是Bean? Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...

  9. select,epoll,poll比较

    介绍和比较 http://www.cnblogs.com/maociping/p/5132583.html 比较 http://www.dataguru.cn/thread-336032-1-1.ht ...

  10. ASP.NET MVC5+EF6+EasyUI 后台管理系统(47)-工作流设计-补充

    系列目录 补充一下,有人要表单的代码,这个用代码生成器生成表Flow_Form表的Index代码就可以 加上几个按钮就可以了 <div class="mvctool"> ...