很早之前看到过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. 一些关于Linux入侵应急响应的碎碎念

    近半年做了很多应急响应项目,针对黑客入侵.但疲于没有时间来总结一些常用的东西,寄希望用这篇博文分享一些安全工程师在处理应急响应时常见的套路,因为方面众多可能有些杂碎. 个人认为入侵响应的核心无外乎四个 ...

  2. MySQL数据库罕见的BUG——Can't get hostname for your address

    在连接mysql jdbc时候,抛出了 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communicat ...

  3. ORACLE分区表梳理系列(二)- 分区表日常维护及注意事项(红字需要留意)

    版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...

  4. hasOwnProperty()、propertyIsEnumerable()和isPrototypeOf()的用法

    javascript中有原型这么一个概念,任何一个构造函数都有它对应的原型(prototype),我们可以给这个原型赋予一些我们想要的属性,像下面这样: function Gadget(name, c ...

  5. Maven入门详解

    什么是Maven Maven,鼎鼎大名,在今天之前,我对于它一直是处于一种"只闻其名不见其人"的状态.之所以说"只闻其名",是因为Maven太有名了,它是Apa ...

  6. MyBatis2:config.xml文件

    前言 前一篇文章,讲了MyBatis入门,讲到了MyBatis有两个基本的配置文件,一个用来配置环境信息,一个用来写SQL语句.前者我把它命名为config.xml,config.xml的内容是: & ...

  7. ucos实时操作系统学习笔记——内核结构和任务创建

    对于ucos实时操作系统,邵贝贝的那本书已经写得很详细了,我因为之前不深的研究过ucos,所以在这里做一个笔记,写一些个人对该操作系统的理解,仅仅是个人理解,如果有人看到这边随笔有不对的地方,望给我指 ...

  8. 2016年我们重新思考移动互联网创业的风险, 微信还是APP?

    感觉这两年前端开发又火起来了,很多做内容创业和做微电商创业的人,往往都选择了运营微信号.对于做纯技术开发的人来说,一般是看不上微信号的,感觉没什么技术含量,或者说没什么技术壁垒.也有另一批人观点相反的 ...

  9. PropertyGrid控件由浅入深(一):文章大纲

    Winform中PropertyGrid控件是一个非常好用的对象属性编辑工具,对于Key-Value形式的数据的处理也是非常的好用. 因为Property控件设计良好,在很小的空间内可以展示很多的内容 ...

  10. Java 8函数编程轻松入门(五)并行化(parallel)

    1.并发与并行的区别 并发: 一个时间段内有几个程序都处于已启动到运行完毕之间,且这几个程序都是在同一个处理机上运行.但在任一个时刻点只有一个程序在处理机上运行 并行: 在同一个时刻,多核处理多个任务 ...