前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用RestSharp 库消费Restful Service

使用RestSharp 库消费Restful Service

作者头像
张善友
发布2018-01-22 11:36:04
1.3K0
发布2018-01-22 11:36:04
举报
文章被收录于专栏:张善友的专栏张善友的专栏

现在互联网上的服务接口都是Restful的,SOAP的Service已经不是主流。.NET/Mono下如何消费Restful Service呢,再也没有了方便的Visual Studio的方便生产代理的工具了,你还在用HttpWebRequest 自己封装吗?Restful Service还有授权问题,自己写出来的代码是不是很不优雅?通常Restful Service返回的数据格式是XML或者Json,还要设置服务的输入参数等等,使用起来很复杂。本文向你推荐一个开源的库RestSharp轻松消费Restful Service。RestSharp是一个开源的.NET平台下REST和Http API的客户端库,支持的平台有.NET 3.5/4、Mono、Mono for Android、MonoTouch、Windows Phone 7.1 Mango。他可以简化我们访问Restful服务,可以到这里下载代码 https://github.com/johnsheehan/RestSharp/archives/master 更简单的使用NuGet。RestSharp使用Json.Net处理 Json数据同Poco对象的序列化。

下面分别从库的使用方式上进行介绍,使用的Restful Service是腾讯社区开放平台(http://opensns.qq.com/)。 1、服务认证,RestSharp定义了一个认证授权的接口 IAuthenticator ,有NtlmAuthenticator、HttpBasicAuthenticator、OAuth1Authenticator、OAuth2Authenticator几种,基本上可以满足要求了,腾讯社区开放平台使用OAuth2,腾讯社区开放平台额外增加了一个OpenId的参数,我们从OAuth2Authenticator的基类继承实现一个:

代码语言:js
复制
    public class OAuthUriQueryParameterAuthenticator : OAuth2Authenticator 
    { 
        private readonly string openId; 
        private readonly string consumerKey;
        public OAuthUriQueryParameterAuthenticator(string openId, string accessToken, string consumerkey) 
            :base(accessToken) 
        { 
            this.openId = openId; 
            this.consumerKey = consumerkey; 
        }
        public override void Authenticate(IRestClient client, IRestRequest request) 
        { 
            request.AddParameter("access_token", AccessToken, ParameterType.GetOrPost); 
            request.AddParameter("openid", openId, ParameterType.GetOrPost); 
            request.AddParameter("oauth_consumer_key", consumerKey, ParameterType.GetOrPost); 
        }

2、Get请求方法,下面的例子是根据access_token获得对应用户身份的openid: https://graph.qq.com/oauth2.0/me?access_token=YOUR_ACCESS_TOKEN

代码语言:js
复制
     public string GetOpenId(string accessToken) 
      {
          RestClient  _restClient = new RestClient(Endpoints.ApiBaseUrl); 
          var request = _requestHelper.CreateOpenIDRequest(accessToken); 
          var response = Execute(request); 
          var openid = GetUserOpenId(response.Content); 
          return openid; 
      }
       private RestSharp.RestResponse Execute(RestRequest request) 
       {
       //返回的结果
           var response = _restClient.Execute(request);
           if (response.StatusCode != HttpStatusCode.OK) 
           { 
               throw new QzoneException(response); 
           } 
           return response; 
       }
       internal RestRequest CreateOpenIDRequest(string accesstoken) 
       { 
           var request = new RestRequest(Method.GET); 
           request.Resource = "oauth2.0/me?access_token={accesstoken}"; 
           request.AddParameter("accesstoken", accesstoken, ParameterType.UrlSegment); 
           return request; 
       }

      上面代码里涉及到了服务的输入参数通过AddParameter方法很方便的处理,是不是很简单。

3、POST请求服务,下面的例子是发表一条微博信息(纯文本)到腾讯微博平台上http://wiki.opensns.qq.com/wiki/%E3%80%90QQ%E7%99%BB%E5%BD%95%E3%80%91add_t

代码语言:js
复制
        /// <summary> 
        /// 发表一条微博信息(纯文本)到腾讯微博平台上 
        /// </summary> 
        /// <param name="content">表示要发表的微博内容。必须为UTF-8编码,最长为140个汉字,也就是420字节。 
        /// 如果微博内容中有URL,后台会自动将该URL转换为短URL,每个URL折算成11个字节。</param> 
        /// <param name="clientip">用户ip,以分析用户所在地</param> 
        /// <param name="jing">用户所在地理位置的经度。为实数,最多支持10位有效数字。有效范围:-180.0到+180.0,+表示东经,默认为0.0</param> 
        /// <param name="wei">用户所在地理位置的纬度。为实数,最多支持10位有效数字。有效范围:-90.0到+90.0,+表示北纬,默认为0.0。</param> 
        /// <param name="syncflag">标识是否将发布的微博同步到QQ空间(0:同步; 1:不同步;),默认为0.</param> 
        /// <returns></returns> 
        internal AddWeiboResult AddWeibo(string content, string clientip = "", string jing = "", string wei = "", int syncflag = 0) 
        {
        RestClient  _restClient = new RestClient(Endpoints.ApiBaseUrl);
             _restClient.Authenticator = new OAuthUriQueryParameterAuthenticator(context.AccessToken.OpenId, context.AccessToken.AccessToken, context.Config.GetAppKey()); 
            var request = _requestHelper.CreateAddWeiboRequest(content, clientip,jing,wei,syncflag);
            var response = Execute(request);            
            var payload = Deserialize<AddWeiboResult>(response.Content); 
            return payload; 
        }
       internal RestRequest CreateAddWeiboRequest(string content, string clientip, string jing, string wei, int syncflag) 
        { 
            var request = new RestRequest(Method.POST); 
            request.RequestFormat = DataFormat.Json; 
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); 
            request.Resource = "t/add_t"; 
            request.AddParameter("content", content); 
            if (!string.IsNullOrEmpty(clientip)) 
            { 
                request.AddParameter("clientip", clientip); 
            } 
            if (!string.IsNullOrEmpty(jing)) 
            { 
                request.AddParameter("jing", jing); 
            } 
            if (!string.IsNullOrEmpty(wei)) 
            { 
                request.AddParameter("wei", wei); 
            } 
            request.AddParameter("syncflag", syncflag); 
            return request; 
        }

   这个方法需要使用到OAuth2的认证和前面的不需要认证的接口比较起来并没有变复杂,代码很优雅。

4、来点复杂的,发个图片微博,RestSharp对HttpFile的封装也很不错,使用起来一样很简单,看代码中的红色部分:

代码语言:js
复制
internal RestRequest CreateAddPictureWeiboRequest(string content, string clientip, string jing, string wei, int syncflag, string fileName, byte[] bytes) 
       { 
           var request = new RestRequest(Method.POST); 
           request.RequestFormat = DataFormat.Json; 
           var boundary = string.Concat("--", Util.GenerateRndNonce()); 
           request.AddHeader("Content-Type", string.Concat("multipart/form-data; boundary=", boundary)); 
           request.Resource = "t/add_pic_t"; 
           request.AddParameter("content", content); 
           if (!string.IsNullOrEmpty(clientip)) 
           { 
               request.AddParameter("clientip", clientip); 
           } 
           if (!string.IsNullOrEmpty(jing)) 
           { 
               request.AddParameter("jing", jing); 
           } 
           if (!string.IsNullOrEmpty(wei)) 
           { 
               request.AddParameter("wei", wei); 
           } 
           request.AddParameter("syncflag", syncflag); 
 request.AddFile("pic", bytes, fileName); 
           return request; 
       }

上面这几个API的调用已经很具有代表性了,是不是可以很好的简化你使用Restful Service,记住DRY(don’t repeat yourself),可以很好的加速你的应用的开发。

Consuming ASP.NET Web API with RestSharp

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-01-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档