C# 服务端篇之实现RestFul Service开发(简单实用)
上篇文章《c# 服务端篇之实现restful service开发(简单实用)》讲解到,如果开发一个简单的restful风格的service,也提到了简单创建一个restful client去如何调用service的api,本文只要再次详细讲解一个高效便捷易扩展的restful client帮助类,就是restsharp,如果只是想自己简单实现一个restful client的方法,可以参考笔者上篇文章讲解的restful service提到的测试client的demo,好了,下面主要讲解一下今天的主(猪)角(脚)—restsharp。
一、restsharp简绍
restsharp是一个轻量的,不依赖任何第三方的组件或者类库的http的组件。restsharp具体以下特性;
1、支持.net 3.5+,silverlight 4, windows phone 7, mono, monotouch, mono for android, compact framework 3.5等
2、通过nuget方便引入到任何项目 ( install-package restsharp )
3、可以自动反序列化xml和json
4、支持自定义的序列化与反序列化
5、自动检测返回的内容类型
6、支持http的get, post, put, head, options, delete等操作
7、可以上传多文件
8、支持oauth 1, oauth 2, basic, ntlm and parameter-based authenticators等授权验证等
9、支持异步操作
10、极易上手并应用到任何项目中
以上是restsharp的主要特点,通用它你可以很容易地用程序来处理一系列的网络请求(get, post, put, head, options, delete),并得到返回结果
github地址--->传送门,下载源码,根据不同的平台架构,编译出对应的dll文件。如图所示:
下面是官方的应用示例,使用起来简单快捷:
1 var client = new restclient("http://example.com"); 2 // client.authenticator = new httpbasicauthenticator(username, password); 3 4 var request = new restrequest("resource/{id}", method.post); 5 request.addparameter("name", "value"); // adds to post or url querystring based on method 6 request.addurlsegment("id", "123"); // replaces matching token in request.resource 7 8 // add parameters for all properties on an object 9 request.addobject(object); 10 11 // or just whitelisted properties 12 request.addobject(object, "personid", "name", ...); 13 14 // easily add http headers 15 request.addheader("header", "value"); 16 17 // add files to upload (works with compatible verbs) 18 request.addfile("file", path); 19 20 // execute the request 21 irestresponse response = client.execute(request); 22 var content = response.content; // raw content as string 23 24 // or automatically deserialize result 25 // return content type is sniffed but can be explicitly set via restclient.addhandler(); 26 irestresponse<person> response2 = client.execute<person>(request); 27 var name = response2.data.name; 28 29 // or download and save file to disk 30 client.downloaddata(request).saveas(path); 31 32 // easy async support 33 await client.executeasync(request); 34 35 // async with deserialization 36 var asynchandle = client.executeasync<person>(request, response => { 37 console.writeline(response.data.name); 38 }); 39 40 // abort the request on demand 41 asynchandle.abort();
二、restsharp应用实例
我们使用上篇文章的restful service的demo进行简单测试一下,新建一个控制台项目,引用生成好的restsharp版本,我的是.net framework 4.5.2 框架,大家根据自身实际情况编译不通的版本进行引用。主要代码如下:
1 using system; 2 using system.collections.generic; 3 using system.linq; 4 using system.text; 5 using system.threading.tasks; 6 using newtonsoft.json; 7 using restsharp; 8 9 namespace restfulclient 10 { 11 class program 12 { 13 static void main(string[] args) 14 { 15 console.title = "restful客户端第三方restsharpdemo测试"; 16 //方法二、使用第三方restsharp 17 var client = new restsharp.restclient("http://127.0.0.1:7788"); 18 var requestget = new restrequest("personinfoquery/{name}", method.get); 19 requestget.addurlsegment("name", "王二麻子"); 20 irestresponse response = client.execute(requestget); 21 var contentget = response.content; 22 console.writeline("get方式获取结果:" + contentget); 23 24 var requestpost = new restrequest("personinfoquery/info", method.post); 25 info info = new info(); 26 info.id = 1; 27 info.name = "张三"; 28 var json = jsonconvert.serializeobject(info); 29 requestpost.addparameter("application/json", json, parametertype.requestbody); 30 irestresponse responsepost = client.execute(requestpost); 31 var contentpost = responsepost.content; 32 console.writeline("post方式获取结果:" + contentpost); 33 console.read(); 34 } 35 } 36 37 [serializable] 38 public class info 39 { 40 public int id { get; set; } 41 public string name { get; set; } 42 } 43 }
开启restful service服务端,编译运行,结果如下:
至此、restsharp的简单应用就介绍这里了,后期将进一步介绍基于restsharp的完善和二次扩展
ps:如有疑问,请留言,未经允许,不得私自转载,转载请注明出处:
上一篇: 数据结构算法(数组翻转对)
下一篇: 脑萎缩的常见表现有哪些 日常需要怎样护理