当前位置: 首页>>代码示例>>C#>>正文


C# IRestRequest.GetGetOrPostParameters方法代码示例

本文整理汇总了C#中IRestRequest.GetGetOrPostParameters方法的典型用法代码示例。如果您正苦于以下问题:C# IRestRequest.GetGetOrPostParameters方法的具体用法?C# IRestRequest.GetGetOrPostParameters怎么用?C# IRestRequest.GetGetOrPostParameters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IRestRequest的用法示例。


在下文中一共展示了IRestRequest.GetGetOrPostParameters方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BuildUrl

 /// <summary>
 /// Build the full URL for a request
 /// </summary>
 /// <param name="client">The REST client that will execute the request</param>
 /// <param name="request">The REST request</param>
 /// <param name="withQuery">Should the resulting URL contain the query?</param>
 /// <returns>Resulting URL</returns>
 /// <remarks>
 /// The resulting URL is a combination of the REST client's BaseUrl and the REST requests
 /// Resource, where all URL segments are replaced and - optionally - the query parameters
 /// added.
 /// </remarks>
 public static Uri BuildUrl(this IRestClient client, IRestRequest request, bool withQuery = true)
 {
     var resource = request.Resource;
     foreach (var param in request.Parameters.Where(x => x.Type == ParameterType.UrlSegment))
     {
         var searchText = string.Format("{{{0}}}", param.Name);
         var replaceText = string.Format("{0}", param.Value);
         resource = resource.Replace(searchText, replaceText);
     }
     var urlBuilder = new UriBuilder(new Uri(client.BaseUrl, new Uri(resource, UriKind.RelativeOrAbsolute)));
     if (withQuery)
     {
         var queryString = new StringBuilder(urlBuilder.Query ?? string.Empty);
         var startsWithQuestionmark = queryString.ToString().StartsWith("?");
         foreach (var param in request.Parameters.Where(x => x.Type == ParameterType.QueryString))
         {
             if (queryString.Length > (startsWithQuestionmark ? 1 : 0))
                 queryString.Append("&");
             queryString.AppendFormat("{0}={1}", Uri.EscapeUriString(param.Name), Uri.EscapeUriString(string.Format("{0}", param.Value)));
         }
         if (request.GetEffectiveHttpMethod() == HttpMethod.Get)
         {
             var getOrPostParameters = request.GetGetOrPostParameters().ToList();
             foreach (var param in getOrPostParameters)
             {
                 if (queryString.Length > (startsWithQuestionmark ? 1 : 0))
                     queryString.Append("&");
                 queryString.AppendFormat("{0}={1}", Uri.EscapeUriString(param.Name), Uri.EscapeUriString(string.Format("{0}", param.Value)));
             }
         }
         urlBuilder.Query = queryString.ToString();
     }
     else
     {
         urlBuilder.Query = string.Empty;
     }
     return urlBuilder.Uri;
 }
开发者ID:changsunfung,项目名称:RestSharp.Portable,代码行数:50,代码来源:RestClientExtensions.cs


注:本文中的IRestRequest.GetGetOrPostParameters方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。