RestSharp_restbed

RestSharp_restbedRestSharp介绍RestSharp是一个.NET平台下REST和HTTPAPI的开源客户端库,支持的平台包括.NET3.5/4、Mono、MonoforAndroid、MonoTouch、WindowsPhone7.1Mango、WindowsPhone8.1。RestSharp可以简化用户访问Restful的服务过程,在这里下载代码可以让用户更简单的使用RestSha…

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

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

RestSharp介绍

RestSharp是一个.NET平台下REST和HTTP API的开源客户端库,支持的平台包括

.NET 3.5/4、Mono、Mono for Android、MonoTouch、Windows Phone 7.1 Mango、Windows Phone 8.1。RestSharp可以简化用户访问Restful的服务过程,在这里下载代码可以让用户更简单的使用RestSharp。RestSharp使用Json.Net处理Json数据同Poco对象的序列化。另外,RestSharp还具有较好的封装性和简化性能。RestSharp的优点:

  • 支持 .NET 3.5+,Silverlight 4, Windows Phone 7, Mono, MonoTouch, Mono for Android, Compact Framework 3.5等

  • 通过NuGet方便引入到任何项目

  • 可以自动反序列化XML和JSON

  • 支持自定义的序列化与反序列化

  • 自动检测返回的内容类型

  • 支持HTTP的GET, POST, PUT, HEAD, OPTIONS, DELETE等操作

  • 可以上传多文件

  • 支持oAuth 1, oAuth 2, Basic, NTLM and Parameter-based Authenticators等授权验证等

  • 支持异步操作

  • 极易上手并应用到任何项目中

链接

- 项目主页: http://restsharp.org
- 下载地址: http://github.com/johnsheehan/RestSharp/downloads
- 项目托管地址:
    - GitRepository: git://github.com/restsharp/RestSharp.git
    - Github项目地址: https://github.com/restsharp/RestSharp
    - Nuget:[https://www.nuget.org/packages/RestSharp](https://www.nuget.org/packages/RestSharp)
- Wiki:[https://github.com/restsharp/RestSharp/wiki](https://github.com/restsharp/RestSharp/wiki)
- stackoverflow:[http://stackoverflow.com/questions/tagged/RestSharp](http://stackoverflow.com/questions/tagged/RestSharp)
- 其他:http://www.cnblogs.com/shanyou/archive/2012/01/27/RestSharp.html

RestSharp帮助类

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using Newtonsoft.Json;
using RestSharp;

namespace Project.Helper
{
    public static class RestHelper
    {
        /// <summary>
        /// params 
        /// </summary>
        static RestClient _restClient;

        static RestHelper()
        {
            _restClient = new RestClient {Timeout = 5000};
        }

        /// <summary>
        /// 基地址
        /// </summary>
        public static Uri BaseUrl
        {
            set { _restClient.BaseUrl = value; }
        }



        /// <summary>
        /// GET
        /// </summary>
        /// <param name="resource">资源</param>
        /// <returns>IRestResponse</returns>
        public static IRestResponse Get(string resource)
        {
            try
            {
                var request = new RestRequest(resource, Method.GET);
                IRestResponse response = _restClient.Execute(request);
                return response;
            }
            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return null;
            }
        }



        /// <summary>
        /// GET
        /// 一般返回string或Enum
        /// </summary>
        /// <param name="resource">资源</param>
        /// <param name="obj">返回对象</param>
        /// <returns>true/false</returns>
        public static bool Get(string resource, out object obj)
        {
            try
            {
                IRestResponse response = Get(resource);
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    obj = JsonConvert.DeserializeObject(response.Content);
                    return true;
                }
                else
                {
                    obj = null;
                    return false;
                }
            }
            catch (Exception ex)
            {
                Log.RecordEx(ex);
                obj = null;
                return false;
            }
        }


        /// <summary>
        /// GET返回T类型对象
        /// </summary>
        /// <typeparam name="T">存在无参构造函数</typeparam>
        /// <param name="resource">资源</param>
        /// <returns>T</returns>
        public static T Get<T>(string resource) where T : new()
        {
            try
            {
                var request = new RestRequest(resource, Method.GET);
                IRestResponse<T> response = _restClient.Execute<T>(request);

                if (response.StatusCode != HttpStatusCode.OK)
                    return default(T);

                if (response.Data != null)
                    return response.Data;

                return JsonConvert.DeserializeObject<T>(response.Content);
            }
            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return default(T);
            }
        }

        /// <summary>
        /// GET返回T类型对象
        /// </summary>
        /// <typeparam name="T">存在无参构造函数</typeparam>
        /// <param name="resource">资源</param>
        /// <param name="isData">是否使用IRestResponse<T>.Data</param>
        /// <returns>T</returns>
        public static T Get<T>(string resource,bool isData) where T : new()
        {
            try
            {
                var request = new RestRequest(resource, Method.GET);
                IRestResponse<T> response = _restClient.Execute<T>(request);

                if (response.StatusCode != HttpStatusCode.OK)
                    return default(T);
                if(isData)
                {
                    if (response.Data != null)
                        return response.Data;
                }

                return JsonConvert.DeserializeObject<T>(response.Content);
            }
            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return default(T);
            }
        }



        /// <summary>
        /// GET返回T类型对象
        /// </summary>
        /// <typeparam name="T">存在无参构造函数的类型</typeparam>
        /// <param name="resource">资源</param>
        /// <param name="parameters">参数列表</param>
        /// <returns>T</returns>
        public static T Get<T>(string resource, List<Parameter> parameters) where T : new()
        {
            try
            {
                var request = new RestRequest(resource, Method.GET);
                foreach (var param in parameters)
                {
                    request.AddParameter(param);
                }
                IRestResponse<T> response = _restClient.Execute<T>(request);
                if (response.Data != null)
                    return response.Data;
                    
                return JsonConvert.DeserializeObject<T>(response.Content);

            }

            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return default(T);
            }
        }



        /// <summary>
        /// GET返回T类型对象
        /// </summary>
        /// <typeparam name="T">存在无参构造函数的类型</typeparam>
        /// <param name="resource">资源</param>
        /// <param name="queryParam">参数列表</param>
        /// <param name="isData">是否使用response.Data</param>
        /// <returns>T</returns>
        public static T Get<T>(string resource, Dictionary<string,string> queryParam,bool isData=false) where T : new()
        {
            try
            {
                var request = new RestRequest(resource, Method.GET);
                foreach (var param in queryParam)
                {
                    request.AddQueryParameter(param.Key, param.Value);
                }
                    
                var response = _restClient.Execute<T>(request);

                if (isData)
                    return response.Data;

                return JsonConvert.DeserializeObject<T>(response.Content);
            }
            catch (Exception ex)
            {

                Log.RecordEx(ex);
                return default(T);
            }

        }



        /// <summary>
        /// POST
        /// </summary>
        /// <param name="resource">resource</param>
        /// <param name="obj">body参数</param>
        /// <returns>HttpStatusCode</returns>
        public static HttpStatusCode Post(string resource, object obj)
        {
            try
            {
                var request = new RestRequest(resource, Method.POST) { RequestFormat = DataFormat.Json };

                request.AddHeader("Content-Type", "application/json"); //设置HTTP头
                request.AddBody(obj);//account实体类的一个对象
                return _restClient.Execute(request).StatusCode;
            }
            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return 0;
            }
        }


        /// <summary>
        /// POST
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="values"></param>
        /// <param name="silence"></param>
        /// <returns></returns>
        public static HttpStatusCode Post(string resource, Dictionary<string, object> values)
        {
            try
            {
                IRestRequest request = new RestRequest(resource, Method.POST);
                foreach (var each in values)
                {
                    request.AddParameter(each.Key, each.Value);
                }

                IRestResponse response = _restClient.Execute(request);
                return response.StatusCode;
            }
            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return 0;
            }



        }

        /// <summary>
        /// PUT
        /// 测试通过
        /// </summary>
        /// <param name="resource">资源</param>
        /// <param name="obj">Body</param>
        /// <returns>HttpStatusCode</returns>
        public static HttpStatusCode Put(string resource,object obj)
        {
            try
            {
                var request = new RestRequest(resource, Method.PUT) { RequestFormat = DataFormat.Json };
                request.AddHeader("Content-Type", "application/json"); //设置HTTP头
                request.AddBody(obj);

                IRestResponse response = _restClient.Execute(request);
                HttpStatusCode result = response.StatusCode;
                return result;
            }
            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return 0;
            }

        }

        /// <summary>
        /// DELETE
        /// </summary>
        /// <param name="resource">资源</param>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        public static HttpStatusCode Delete(string resource,object obj)
        {
            try
            {
                var request = new RestRequest(resource, Method.DELETE) { RequestFormat = DataFormat.Json };
                request.AddHeader("Content-Type", "application/json"); //设置HTTP头
                request.AddBody(obj);//account实体类的一个对象
                IRestResponse response = _restClient.Execute(request);
                HttpStatusCode result = response.StatusCode;
                return result;
            }
            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return 0;
            }

        }


        public static HttpStatusCode Delete(string resource)
        {
            try
            {
                var request = new RestRequest(resource, Method.DELETE) { RequestFormat = DataFormat.Json };
                request.AddHeader("Content-Type", "application/json"); //设置HTTP头
                IRestResponse response = _restClient.Execute(request);
                HttpStatusCode result = response.StatusCode;

                return result;
            }

            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return 0;
            }
        }



        /// <summary>
        /// PUT
        /// 测试通过
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="values"></param>
        /// <param name="silence"></param>
        /// <returns></returns>
        public static bool Put(string resource, Dictionary<string, object> values, bool silence = false)
        {
            try
            {
                IRestRequest request = new RestRequest(resource, Method.PUT);
                foreach (var each in values)
                {
                    request.AddParameter(each.Key, each.Value);
                }
                IRestResponse response = _restClient.Execute(request);
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return false;
            }

        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="resource">资源</param>
        /// <param name="path">文件路径</param>
        /// <returns>成功:true/失败:false</returns>
        public static bool Put(string resource, string path)
        {
            try
            {
                var request = new RestRequest(resource, Method.PUT) { RequestFormat = DataFormat.Json };
                request.AddHeader("Content-Disposition", "form-data");
                request.AddFile("file", path);

                IRestResponse response = _restClient.Execute(request);
                return response.StatusCode == HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                Log.RecordEx(ex);
                return false;
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/195941.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • C++进制转换(十进制转二进制、八进制、随意进制)

    C++进制转换(十进制转二进制、八进制、随意进制)

    2021年12月14日
  • dfile.encoding=utf-8 乱码_OrdosTF格式是什么格式

    dfile.encoding=utf-8 乱码_OrdosTF格式是什么格式解析ASN10x80问题所有X509证书都用一种ASN1格式,其为一种嵌套储存格式。ASN1规则繁琐,此文不说明,可以研究此链接。本文主要说明0x80问题,同事问起这问题,一时想不起来,现在在此备份。所有ASN1格式都是0x30开头,表示ASN1tag;0x82用来表示03C2的长度,先搁置;而03C2其用来表示后面数据长度,如下所示。308203C

  • winscp连接虚拟机Linux被拒绝的问题解决方案[通俗易懂]

    winscp连接虚拟机Linux被拒绝的问题解决方案[通俗易懂]winscp连接虚拟机Linux被拒绝的问题解决方案

  • 内核态和用户态区别的重要性_cpu用户态和内核态区别

    内核态和用户态区别的重要性_cpu用户态和内核态区别内核态和用户态区别内核态和用户态区别当一个任务(进程)执行系统调用而陷入内核代码中执行时,我们就称进程处于内核运行态(或简称为内核态)。此时处理器处于特权级最高的(0级)内核代码中执行。当进程处于内核态时,执行的内核代码会使用当前进程的内核栈。每个进程都有自己的内核栈。当进程在执行用户自己的代码时,则称其处于用户运行态(用户态)。即此时处理器在特权级最低的(3级)用户代码中运行。当正在

  • android 如何打开s3db,Android开发教程:使用已有的SQL数据库

    android 如何打开s3db,Android开发教程:使用已有的SQL数据库极品好儿媳全文阅读,燃料乙醇放量,嘉兴办公家具,应向阳,58会展网,证书挂靠,鱼缸清洗,qq蜡笔小新头像我在sqliteadmin创建好数据库StuDB,里面的表如下:650)this.width=650;”>将创建好的数据库在DDMS中点击650)this.width=650;”>导入到data/data/程序的包名/650)this.width=650;”>SQLit…

  • VM虚拟机安装教程_ghost手动安装教程

    VM虚拟机安装教程_ghost手动安装教程VMware最新官方下载与安装目录一、VMware官方下载二、虚拟机安装一、VMware官方下载首先我们访问官网地址https://www.vmware.com/cn.html注意:没有账号必须先注册才能下载。注册页面https://my.vmware.com/cn/web/vmware/registration注册完账号后进行以下步骤:如图,选择下载专…

发表回复

您的电子邮箱地址不会被公开。

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