本文整理汇总了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;
}