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


C# IRestRequest.GetBodyContent方法代码示例

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


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

示例1: GetBasicContent

        /// <summary>
        /// Gets the basic content (without files) for a request
        /// </summary>
        /// <param name="client">The REST client that will execute the request</param>
        /// <param name="request">REST request to get the content for</param>
        /// <returns>The HTTP content to be sent</returns>
        internal static IHttpContent GetBasicContent([CanBeNull] this IRestClient client, IRestRequest request)
        {
            IHttpContent content;
            var parameters = client.MergeParameters(request);
            var body = parameters.FirstOrDefault(x => x.Type == ParameterType.RequestBody);
            if (body != null)
            {
                content = request.GetBodyContent(body);
            }
            else
            {
                if (client.GetEffectiveHttpMethod(request) == Method.POST)
                {
                    var getOrPostParameters = parameters.GetGetOrPostParameters().ToList();
                    if (getOrPostParameters.Count != 0)
                    {
                        content = new PostParametersContent(getOrPostParameters);
                    }
                    else
                    {
                        content = null;
                    }
                }
                else
                {
                    content = null;
                }
            }

            return content;
        }
开发者ID:ReachContact,项目名称:restsharp.portable,代码行数:37,代码来源:RestClientExtensions.cs

示例2: GetBasicContent

        /// <summary>
        /// Gets the basic content (without files) for a request
        /// </summary>
        /// <param name="client">The REST client that will execute the request</param>
        /// <param name="request">REST request to get the content for</param>
        /// <param name="parameters">The request parameters for the REST request (read-only)</param>
        /// <returns>The HTTP content to be sent</returns>
        private static HttpContent GetBasicContent([CanBeNull] this IRestClient client, IRestRequest request, RequestParameters parameters)
        {
            HttpContent content;
            var body = parameters.OtherParameters.FirstOrDefault(x => x.Type == ParameterType.RequestBody);
            if (body != null)
            {
                content = request.GetBodyContent(body);
            }
            else
            {
                var effectiveMethod = client.GetEffectiveHttpMethod(request);
                if (effectiveMethod != Method.GET)
                {
                    var getOrPostParameters = parameters.OtherParameters.GetGetOrPostParameters().ToList();
                    if (getOrPostParameters.Count != 0)
                    {
                        content = new PostParametersContent(getOrPostParameters).AsHttpContent();
                    }
                    else
                    {
                        content = null;
                    }
                }
                else
                {
                    content = null;
                }
            }

            return content;
        }
开发者ID:yuleyule66,项目名称:restsharp.portable,代码行数:38,代码来源:RestClientExtensions.cs

示例3: GetMultiPartContent

        /// <summary>
        /// Gets the multi-part content (with files) for a request
        /// </summary>
        /// <param name="client">The REST client that will execute the request</param>
        /// <param name="request">REST request to get the content for</param>
        /// <param name="parameters">The merged request parameters</param>
        /// <returns>The HTTP content to be sent</returns>
        private static IHttpContent GetMultiPartContent([CanBeNull] this IRestClient client, IRestRequest request, RequestParameters parameters)
        {
            var isPostMethod = client.GetEffectiveHttpMethod(request, parameters.OtherParameters) == Method.POST;
            var multipartContent = new MultipartFormDataContent(new GenericHttpHeaders());
            foreach (var parameter in parameters.OtherParameters)
            {
                var fileParameter = parameter as FileParameter;
                if (fileParameter != null)
                {
                    var file = fileParameter;
                    var data = new ByteArrayContent((byte[])file.Value);
                    if (!string.IsNullOrEmpty(file.ContentType))
                    {
                        data.Headers.ReplaceWithoutValidation("Content-Type", file.ContentType);
                    }

                    data.Headers.ReplaceWithoutValidation("Content-Length", file.ContentLength.ToString());
                    multipartContent.Add(data, file.Name, file.FileName);
                }
                else if (isPostMethod && parameter.Type == ParameterType.GetOrPost)
                {
                    IHttpContent data;
                    var bytes = parameter.Value as byte[];
                    if (bytes != null)
                    {
                        var rawData = bytes;
                        data = new ByteArrayContent(rawData);
                        data.Headers.ReplaceWithoutValidation(
                            "Content-Type",
                            string.IsNullOrEmpty(parameter.ContentType) ? "application/octet-stream" : parameter.ContentType);
                        data.Headers.ReplaceWithoutValidation("Content-Length", rawData.Length.ToString());
                        multipartContent.Add(data, parameter.Name);
                    }
                    else
                    {
                        var value = parameter.ToRequestString();
                        data = new StringContent(value, parameter.Encoding ?? ParameterExtensions.DefaultEncoding);
                        if (!string.IsNullOrEmpty(parameter.ContentType))
                        {
                            data.Headers.ReplaceWithoutValidation("Content-Type", parameter.ContentType);
                        }

                        multipartContent.Add(data, parameter.Name);
                    }
                }
                else if (parameter.Type == ParameterType.RequestBody)
                {
                    var data = request.GetBodyContent(parameter);
                    multipartContent.Add(data, parameter.Name);
                }
            }

            return multipartContent;
        }
开发者ID:yuleyule66,项目名称:restsharp.portable,代码行数:61,代码来源:GenericContentCollector.cs

示例4: GetMultiPartContent

        /// <summary>
        /// Gets the multi-part content (with files) for a request
        /// </summary>
        /// <param name="client">The REST client that will execute the request</param>
        /// <param name="request">REST request to get the content for</param>
        /// <returns>The HTTP content to be sent</returns>
        internal static HttpContent GetMultiPartContent([CanBeNull] this IRestClient client, IRestRequest request)
        {
            var isPostMethod = client.GetEffectiveHttpMethod(request) == Method.POST;
            var multipartContent = new MultipartFormDataContent();
            var parameters = client.MergeParameters(request);
            foreach (var parameter in parameters)
            {
                var fileParameter = parameter as FileParameter;
                if (fileParameter != null)
                {
                    var file = fileParameter;
                    var data = new ByteArrayContent((byte[])file.Value);
                    data.Headers.ContentType = MediaTypeHeaderValue.Parse(file.ContentType);
                    data.Headers.ContentLength = file.ContentLength;
                    multipartContent.Add(data, file.Name, file.FileName);
                }
                else if (isPostMethod && parameter.Type == ParameterType.GetOrPost)
                {
                    HttpContent data;
                    var bytes = parameter.Value as byte[];
                    if (bytes != null)
                    {
                        var rawData = bytes;
                        data = new ByteArrayContent(rawData);
                        data.Headers.ContentType = string.IsNullOrEmpty(parameter.ContentType)
                                                       ? new MediaTypeHeaderValue("application/octet-stream")
                                                       : MediaTypeHeaderValue.Parse(parameter.ContentType);
                        data.Headers.ContentLength = rawData.Length;
                        multipartContent.Add(data, parameter.Name);
                    }
                    else
                    {
                        var value = parameter.ToRequestString();
                        data = new StringContent(value, parameter.Encoding ?? ParameterExtensions.DefaultEncoding);
                        if (!string.IsNullOrEmpty(parameter.ContentType))
                            data.Headers.ContentType = MediaTypeHeaderValue.Parse(parameter.ContentType);

                        multipartContent.Add(data, parameter.Name);
                    }
                }
                else if (parameter.Type == ParameterType.RequestBody)
                {
                    var data = request.GetBodyContent(parameter);
                    multipartContent.Add(data, parameter.Name);
                }
            }

            return multipartContent;
        }
开发者ID:evnik,项目名称:restsharp.portable,代码行数:55,代码来源:RestClientExtensions.cs


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