.NET RESTful Web Services入门
很早之前看到过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入门的更多相关文章
- RESTful Web Services初探
RESTful Web Services初探 作者:杜刚 近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTf ...
- Jersey the RESTful Web Services in Java
Jersey 是一个JAX-RS的实现, JAX-RS即Java API for RESTful Web Services, 支持按照表述性状态转移(REST)架构风格创建Web服务. REST 中最 ...
- 使用 Spring 3 来创建 RESTful Web Services
来源于:https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/ 在 Java™ 中,您可以使用以下几种方法来创建 RESTful We ...
- 就是这么简单!使用Rest-assured 测试Restful Web Services
使用 Rest-assured 测试 Restful Web Services 转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html 这里向大家介 ...
- cxf开发Restful Web Services
一.restful web services rest全称是Representation State Transfer(表述性状态转移).它是一种软件架构风格,只是提供了一组设计原则和约束条件.在re ...
- RESTful Web Services测试工具推荐
命令行控的最爱:cURL cURL是一个很强大的支持各种协议的文件传输工具,用它来进行RESTful Web Services的测试简直是小菜一碟.这个工具基本上类Unix操作系统(各种Linux.M ...
- 【转】RESTful Web Services初探
近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTful风格的Web Services,比较著名的包括Twit ...
- 使用 Spring 3 来创建 RESTful Web Services(转)
使用 Spring 3 来创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参 ...
- 基于Spring设计并实现RESTful Web Services(转)
基于Spring设计并实现RESTful Web Services 在本教程中,你将会使用Spring来创建一个具有生产力的RESTful网络服务. 为什么用RESTful网络服务? 从和Amazon ...
随机推荐
- 简历生成平台项目开发-STEP1问卷设计
周五课程结束完后,小组建立QQ群和微信群,着手讨论项目问题.一开始的大概想法:就业信息平台,收集企业招聘信息和就业生资料,提供给学生和企业的校企对接平台.后来发现群里谭卓同学也有个相关的思路,经过商量 ...
- CentOS下Zabbix安装部署及汉化
搭建环境:Centos6.5_x86_64,Zabbix2.4.5,epel 源 服务端: 1.安装开发软件包yum -y groupinstall "Development Tools&q ...
- 定时Job在IIS中潜在危险-IIS 定期回收
引言 有时我们会在IIS中启用一些定时服务,但是你必须清楚IIS会定期回收Asp.net的应用程序的.首先来看IIS啥时候回收APPDomain. APPDomain 回收时机 There are ...
- Linux测试环境搭建的学习建议
随着Linux应用的扩展许多朋友开始接触Linux,根据学习Windwos的经验往往有一些茫然的感觉:不知从何处开始学起.这里介绍学习Linux测试环境搭建的一些建议. 一.Linux测试环境搭建从基 ...
- jmeter之线程组的使用
线程组 在使用jmeter性能测试时,我们都得先添加个线程组,右键testplan-->添加-->Threads-->线程组.在线程组下执行. 问题:为了能够让jmeter在做性能测 ...
- 一步步学习javascript基础篇(9):ajax请求的回退
需求1: ajax异步请求 url标识请求参数(也就是说复制url在新页面打开也会是ajax后的效果) ajax异步请求没问题,问题一般出在刷新url后请求的数据没了,这就是因为url没有记录参数.如 ...
- 开源一个WEB版本GEF,基于SVG的网页流程图框架
8月开始断断续续的制作这个web gef,没有任何依赖,完全原生js开发,目前已经完成了雏形,基本上可以在项目里应用了. 下图展示的是demo1的效果,包括拖拽,生成连线,点击生成\取消墙体,整个de ...
- 基于DDDLite的权限管理OpenAuth.net 1.0版正式发布
距离上一篇OpenAuth.net的文章已经有5个多月了,在这段时间里项目得到了很多朋友的认可,开源中国上面的Star数接近300,于是坚定了我做下去的信心.最近稍微清闲点,正式推出1.0版,并在阿里 ...
- is和as
一.明确两个基本概念 隐式转换: a.对于值类型,低精度=>高精度.eg:int=>long b.对于引用类型,子类向祖宗类转换过程.eg:对象=>Object 显式转换:显示转换是 ...
- Java中六大时间类的使用和区别
关于java中六个时间类的使用和区别 java.util.Date java.sql.Date java.sql.Time java.sql.Timestamp java.text.SimpleD ...