restsharp.dll_restbed

restsharp.dll_restbed一、RestSharp简绍RestSharp是一个轻量的,不依赖任何第三方的组件或者类库的Http的组件。RestSharp具体以下特性;1、支持.NET3.5+,Silverlight4,WindowsPhone7,Mono,MonoTouch,MonoforAndroid,CompactFramework3.5等  2、通过NuGet方便引入到任何项目(In…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

一、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),并得到返回结果

下面是官方的应用示例,使用起来简单快捷:

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

// add parameters for all properties on an object
request.AddObject(object);

// or just whitelisted properties
request.AddObject(object, "PersonId", "Name", ...);

// easily add HTTP Headers
request.AddHeader("header", "value");

// add files to upload (works with compatible verbs)
request.AddFile("file", 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();
IRestResponse<Person> response2 = client.Execute<Person>(request);
var name = response2.Data.Name;

// or download and save file to disk
client.DownloadData(request).SaveAs(path);

// easy async support
await client.ExecuteAsync(request);

// async with deserialization
var asyncHandle = client.ExecuteAsync<Person>(request, response => {
   Console.WriteLine(response.Data.Name);
});

// abort the request on demand
asyncHandle.Abort();

二、RestSharp应用实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestSharp;

namespace RestFulClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Restful客户端第三方RestSharpDemo测试";
            //方法二、使用第三方RestSharp
            var client = new RestSharp.RestClient("http://127.0.0.1:7788");
            var requestGet = new RestRequest("PersonInfoQuery/{name}", Method.GET);
            requestGet.AddUrlSegment("name", "王二麻子");
            IRestResponse response = client.Execute(requestGet);
            var contentGet = response.Content;
            Console.WriteLine("GET方式获取结果:" + contentGet);

            var requestPost = new RestRequest("PersonInfoQuery/Info", Method.POST);
            Info info = new Info();
            info.ID = 1;
            info.Name = "张三";
            var json = JsonConvert.SerializeObject(info);
            requestPost.AddParameter("application/json", json, ParameterType.RequestBody);
            IRestResponse responsePost = client.Execute(requestPost);
            var contentPost = responsePost.Content;
            Console.WriteLine("POST方式获取结果:" + contentPost);
            Console.Read();
        }
    }

    [Serializable]
    public class Info
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/195840.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux chmod 755

    linux chmod 755chmod是Linux下设置文件权限的命令,后面的数字表示不同用户或用户组的权限。一般是三个数字:第一个数字表示文件所有者的权限第二个数字表示与文件所有者同属一个用户组的其他用户的权限第三个数字表示其它用户组的权限。权限分为三种:读(r=4),写(w=2),执行(x=1)。综合起来还有可读可执行(rx=5=4+1)、可读可写(rw=6=4+2)、可读可写可执行(rwx=7=4+2+1)。所以,ch…

    2022年7月16日
    24
  • Eclipse汉化教程详细[通俗易懂]

    Eclipse汉化教程详细[通俗易懂]eclipse汉化包下载建议从:www.vipkes.cn或www.tkres.cn(恬恪学习网)下载,与此教程配套的资料。或从其它渠道下载。 下载完成后,解压压缩包: 打开压缩包后,可看到里面有个“eclipse”的文件夹,再进入该文件夹,可以看到如下两个文件夹:4.复制这两个文件夹,并找到eclipse安装根目录,如果不知道在哪里,请在桌面鼠标右键—》属性—》快捷方式—》打开文件所在位置即可。在eclipse安装目录下,找到以下dropins文件,双…

    2022年6月6日
    49
  • Linux命令速查手册出炉!

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 来源:MarkerHub 查看Linux系统信息 arch    &nbsp…

    2021年6月26日
    91
  • python3.0菜鸟教程100例_python入门到精通教程完整版

    python3.0菜鸟教程100例_python入门到精通教程完整版Python3100例原题地址:http://www.runoob.com/python/python-100-examples.htmlgit地址:https://github.com/RichardFu123/Python100Cases原例为Python2.7版本重写过程中有不少是随意发挥的重写运行版本:Python3.7总…

    2022年9月20日
    3
  • Android官方文档翻译-Broadcasts

    Android官方文档翻译-Broadcasts原文链接:https://developer.android.com/guide/components/broadcasts.html广播Android应用可以向Android系统和其他Android应用发送或从它们那接收广播消息,这类似于发布-订阅设计模式。当需要关注的事件发生时这些广播就会发送出去。例如Android系统在许多系统事件发生时会发送广播,比如当系统启动或设备开始充电时。应用也可以发

    2022年6月26日
    31
  • 多线程编程 -wait(),notify()/notityAll()方法

    多线程编程 -wait(),notify()/notityAll()方法

    2021年5月6日
    121

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号