.NET的HTTP辅助类:RestSharp
var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);
var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource
// easily add HTTP Headers
request.AddHeader("header", "value");
// add files to upload (works with compatible verbs)
request.AddFile(path);
// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
// or automatically deserialize result
// return content type is sniffed but can be explicitly set via RestClient.AddHandler();
RestResponse<Person> response2 = client.Execute<Person>(request);
var name = response2.Data.Name;
// easy async support
client.ExecuteAsync(request, response => {
Console.WriteLine(response.Content);
});
// async with deserialization
var asyncHandle = client.ExecuteAsync<Person>(request, response => {
Console.WriteLine(response.Data.Name);
});
// abort the request on demand
( 2 )通过NuGet把RestSharp引入项目中
通过RestSharp的相关接口,快速调用RESTful API
以调用DaoCloud的API为例
1 |
// <add key="DaoCloudAPI_BASE_URL" value="https://openapi.daocloud.io/"/> |
以上代码运行结果:
![]()
以上是使用同步方式调用,由于网络通信的不稳定性,建议使用异步方式进行调用:
1 |
client.ExecuteAsync(request,response => |
对于RESTful的返回值json数据,在RestSharp中已经有相关的反序列化实现,当然首先得有json对应的C#实体类,通过在线的一些网站可以很快获得这个类:
http://www.jsons.cn/jsontomodel/
通过以上网站,转换得到C#实体类代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class Package
{
/// <summary>
///
/// </summary>
public string image { get; set; }
/// <summary>
///
/// </summary>
public string id { get; set; }
}
public class App_runtime
{
/// <summary>
/// 云端测试环境
/// </summary>
public string display_name { get; set; }
/// <summary>
///
/// </summary>
public string id { get; set; }
/// <summary>
///
/// </summary>
public string name { get; set; }
}
public class AppItem
{
/// <summary>
///
/// </summary>
public string enable_auto_redeploy { get; set; }
...
有了这些自动生成的JSON对应的实体类就可以使用RestSharp把返回的数据直接转换成对象了,使用如下方法即可:
1
2
3
4
5
6
7 //同步获取返回值
var _result = client.Execute<Model.DaoCloud.Root>(request);
//异步获取
client.ExecuteAsync<Model.DaoCloud.Root>(request, response =>
{
Console.WriteLine(response.Content);
});
.NET的HTTP辅助类:RestSharp的更多相关文章
- RestSharp 一个.NET(C#)的HTTP辅助类组件
互联网上关于.NET(C#)的HTTP相关的辅助类还是比较多的,这里再为大家推荐一个.NET的HTTP辅助类,它叫RestSharp.RestSharp是一个轻量的,不依赖任何第三方的组件或者类库的H ...
- 推荐一个.NET(C#)的HTTP辅助类组件--restsharp
互联网上关于.NET(C#)的HTTP相关的辅助类还是比较多的,这里再为大家推荐一个.NET的HTTP辅助类,它叫RestSharp.RestSharp是一个轻量的,不依赖任何第三方的组件或者类库的H ...
- Restful&RestSharp
RestSharp 一个.NET(C#)的HTTP辅助类组件 https://www.cnblogs.com/lilunjia/p/7090015.html C#使用RestSharp实现post发送 ...
- Java的几个同步辅助类
Java为我们提供了一些同步辅助类,利用这些辅助类我们可以在多线程编程中,灵活地把握线程的状态. CountDownLatch CountDownLatch一个同步辅助类,在完成一组正在其他线程中执行 ...
- ASP.NET Core 中文文档 第四章 MVC(3.6.2 )自定义标签辅助类(Tag Helpers)
原文:Authoring Tag Helpers 作者:Rick Anderson 翻译:张海龙(jiechen) 校对:许登洋(Seay) 示例代码查看与下载 从 Tag Helper 讲起 本篇教 ...
- DateHelper.cs日期时间操作辅助类C#
//==================================================================== //** Copyright © classbao.com ...
- 同步辅助类CountDownLatch用法
CountDownLatch是一个同步辅助类,犹如倒计时计数器,创建对象时通过构造方法设置初始值,调用CountDownLatch对象的await()方法则使当前线程处于等待状态,调用countDow ...
- 基于MemoryCache的缓存辅助类
背景: 1. 什么是MemoryCache? memoryCache就是用电脑内存做缓存处理 2.使用范围? 可用于不常变的数据,进行保存在内存中,提高处理效率 代码: /// <summary ...
- java中被各种XXUtil/XXUtils辅助类恶心到了,推荐这种命名方法
且看一下有多少个StringUtils 列举一下XXUtil/XXUtils恶劣之处 1. 不知道该用XXUtil还是用XXUtils, 或者XXHelper, XXTool 2. 不知道该用a.ja ...
随机推荐
- Pomelo框架
一个典型的多进程MMO运行架构, 如下图所示: pomelo框架的组成如图所示: 架构把游戏服务器做了抽象, 抽象成为两类:前端服务器和后端服务器, 如图: 前端服务器(frontend)的职责: 负 ...
- HDU3652(数位dp)
A - B-number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descri ...
- RedisRepository分享和纠错
.mytitle { background: #2B6695; color: white; font-family: "微软雅黑", "宋体", "黑 ...
- WebForm 控件(一)、连接数据库
一.控件 [简单控件] (一)文字显示 1.Label → 在html中相当于span <asp:Label ID="控件名 runat="server" Tex ...
- 基于Ceph快照的异地灾备设计
作者:吴香伟 发表于 2017/02/06 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 喜欢请点击右边打赏,谢谢支持! 引子 技术改变生活. 越来越方便的手机 ...
- gulp源码解析(三)—— 任务管理
上篇文章我们分别对 gulp 的 .src 和 .dest 两个主要接口做了分析,今天打算把剩下的面纱一起揭开 —— 解析 gulp.task 的源码,了解在 gulp4.0 中是如何管理.处理任务的 ...
- Storyboard 自定义转场动画
在storyboard中,segue有几种不同的类型,在iphone和ipad的开发中,segue的类型是不同的.在iphone中,segue 有:push,modal,和custom三种不同的类型, ...
- .NET十五周年生日快乐 (3月7日发布Visual Studio 2017正式版?)
今天 是.NET 对世界首次亮相15 周年.2002 年 2 月 13 日,第一版本的.NET 发布作为 Visual Studio.NET 的一部分.它仿佛就在昨天为微软建设成"下一代 W ...
- C语言一维数组转换为二维数组
一维转二维代码示例: #include <stdio.h> #include <stdlib.h> #define ROW 3 #define COL 2 int main(i ...
- My97DatePicker.js 之无效日期设置
1.下载并引用My97DatePicker.js 2.将工作日休假的和周末上班的维护到数据库中 3.取出某段时间内的不上班日期集合 /// <summary> ///获取 无效日期集合 1 ...