Пример #1
0
        private static HttpContent GenerateFormPostData(ActionApiDescriptionModel action, IReadOnlyDictionary <string, object> methodArguments)
        {
            var parameters = action
                             .Parameters
                             .Where(p => p.BindingSourceId == ParameterBindingSources.Form || p.BindingSourceId == ParameterBindingSources.FormFile)
                             .ToArray();

            if (!parameters.Any())
            {
                return(null);
            }

            if (parameters.Any(x => x.BindingSourceId == ParameterBindingSources.FormFile))
            {
                var formData = new MultipartFormDataContent();
                foreach (var parameter in parameters)
                {
                    var value = HttpActionParameterHelper.FindParameterValue(methodArguments, parameter);
                    if (value == null)
                    {
                        continue;
                    }

                    if (value is IRemoteStreamContent remoteStreamContent)
                    {
                        var stream = remoteStreamContent.GetStream();
                        if (stream.CanSeek)
                        {
                            stream.Position = 0;
                        }
                        var streamContent = new StreamContent(stream);
                        if (!remoteStreamContent.ContentType.IsNullOrWhiteSpace())
                        {
                            streamContent.Headers.ContentType = new MediaTypeHeaderValue(remoteStreamContent.ContentType);
                        }
                        formData.Add(streamContent, parameter.Name, remoteStreamContent.FileName ?? parameter.Name);
                    }
                    else if (value is IEnumerable <IRemoteStreamContent> remoteStreamContents)
                    {
                        foreach (var content in remoteStreamContents)
                        {
                            var stream = content.GetStream();
                            if (stream.CanSeek)
                            {
                                stream.Position = 0;
                            }
                            var streamContent = new StreamContent(stream);
                            if (!content.ContentType.IsNullOrWhiteSpace())
                            {
                                streamContent.Headers.ContentType = new MediaTypeHeaderValue(content.ContentType);
                            }
                            formData.Add(streamContent, parameter.Name, content.FileName ?? parameter.Name);
                        }
                    }
                    else
                    {
                        formData.Add(new StringContent(value.ToString(), Encoding.UTF8), parameter.Name);
                    }
                }

                return(formData);
            }
            else
            {
                var postDataBuilder = new StringBuilder();

                var isFirstParam = true;
                foreach (var parameter in parameters.Where(p => p.BindingSourceId == ParameterBindingSources.Form))
                {
                    var value = HttpActionParameterHelper.FindParameterValue(methodArguments, parameter);
                    if (value == null)
                    {
                        continue;
                    }

                    postDataBuilder.Append(isFirstParam ? "?" : "&");
                    postDataBuilder.Append(parameter.Name + "=" + System.Net.WebUtility.UrlEncode(value.ToString()));

                    isFirstParam = false;
                }

                return(new StringContent(postDataBuilder.ToString(), Encoding.UTF8, MimeTypes.Application.XWwwFormUrlencoded));
            }
        }