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怎么复制文件到其他文件夹

    Linux怎么复制文件到其他文件夹1.前言本文主要讲解linux怎么复制文件到其他文件夹。在Linux和Unix系统上工作时,复制文件和目录是您每天要执行的最常见任务之一。cp是一个命令行实用程序,用于复制Unix和Linux系统上的文件和目录。在本文中,我们将解释如何使用cp命令。linux怎么复制文件到其他文件夹2.如何使用cp命令cp命令的使用语法:cp[OPTIONS]源…目标源可以有一个或多个文件或目录作为参数,目标可以有一个文件或文件夹作为参数。当源和目标参数都是文件时,cp命令将第一

    2025年6月10日
    5
  • 删除windows默认共享[通俗易懂]

    删除windows默认共享  Windows2000的缺省安装很容易被攻击者取得账号列表,即使安装了最新的Servicepack也是如此。在Windows2000中有一个缺省共享IPC$,并且还有诸如admin$C$D$等等,而IPC$允许匿名用户(即未经登录的用户)访问,利用这个缺省共享可以取得用户列表。要想防范这些,可将在“管理工具→本地安全策略→安全设置→本地策略→

    2022年4月15日
    48
  • OpenCV300 CMake生成project在项目过程中的问题

    OpenCV300 CMake生成project在项目过程中的问题

    2022年1月11日
    40
  • mysql导入导出sql文件

    mysql导入导出sql文件

    2021年10月26日
    49
  • Client ID认证「建议收藏」

    Client ID认证「建议收藏」1.插件ClientID认证使用配置文件预设客户端ClientID与密码,支持通过HTTPAPI管理认证数据。ClientID认证不依赖外部数据源,使用上足够简单轻量,使用该种认

    2022年7月1日
    35
  • linux wget命令「建议收藏」

    linux wget命令「建议收藏」from:http://wenku.baidu.com/view/0854a222192e45361066f571.htmlWGet使用指南wget是一个从网络上自动下载文件的自由工具。它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理.所谓的自动下载是指,wget可以在用户退出系统的之后在后台执行。这意味这你可以登录系统,启动一个wget下载任务,然后退出系统,wget将在后台执行直到任务完成,相对于其它大部分浏览器在下载大量数据时需要用户一直的参与,这省去了极大的麻烦。wg

    2022年5月7日
    46

发表回复

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

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