Пример #1
0
        /// <summary>
        /// 设置请求体
        /// </summary>
        /// <param name="httpMethodAttribute"></param>
        /// <param name="methodParameters"></param>
        /// <param name="request"></param>
        private static void SetHttpRequestBody(HttpMethodAttribute httpMethodAttribute, Dictionary <string, ParameterValue> methodParameters, HttpRequestMessage request)
        {
            // 排除 GET/DELETE 请求
            if (httpMethodAttribute.Method == HttpMethod.Get || httpMethodAttribute.Method == HttpMethod.Delete)
            {
                return;
            }

            // 获取所有非基元类型,该类型当作 Body 参数
            var bodyParameters = methodParameters.Where(u => u.Value.IsBodyParameter);

            if (bodyParameters.Any())
            {
                var bodyJson = JsonSerializer.Serialize(bodyParameters.First().Value.Value, new JsonSerializerOptions
                {
                    PropertyNameCaseInsensitive = true,
                    WriteIndented = true,
                    Encoder       = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                });
                request.Content = new StringContent(bodyJson, Encoding.UTF8, "application/json");

                // 打印请求地址
                App.PrintToMiniProfiler(MiniProfilerCategory, "Body", bodyJson);
            }
        }
Пример #2
0
        /// <summary>
        /// 设置请求体
        /// </summary>
        /// <param name="httpMethodAttribute"></param>
        /// <param name="methodParameters"></param>
        /// <param name="request"></param>
        private static void SetHttpRequestBody(HttpMethodAttribute httpMethodAttribute, Dictionary <string, ParameterValue> methodParameters, HttpRequestMessage request)
        {
            // 排除 GET/Head 请求
            if (httpMethodAttribute.Method == HttpMethod.Get || httpMethodAttribute.Method == HttpMethod.Head)
            {
                return;
            }

            // 获取所有非基元类型,该类型当作 Body 参数
            var bodyParameters = methodParameters.Where(u => u.Value.IsBodyParameter);

            if (bodyParameters.Any())
            {
                // 获取 body 参数
                var bodyArgs = bodyParameters.First().Value.Value;

                string body;

                // 处理 json 类型
                if (httpMethodAttribute.ContentType.Contains("json"))
                {
                    body = JsonSerializerUtility.Serialize(bodyArgs);
                }
                // 处理 xml 类型
                else if (httpMethodAttribute.ContentType.Contains("xml"))
                {
                    var xmlSerializer = new XmlSerializer(bodyArgs.GetType());
                    var buffer        = new StringBuilder();

                    using var writer = new StringWriter(buffer);
                    xmlSerializer.Serialize(writer, bodyArgs);

                    body = buffer.ToString();
                }
                // 其他类型
                else
                {
                    body = bodyArgs.ToString();
                }

                if (!string.IsNullOrEmpty(body))
                {
                    var httpContent = new StringContent(body, Encoding.UTF8);

                    // 设置内容类型
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue(httpMethodAttribute.ContentType);
                    request.Content = httpContent;

                    // 打印请求地址
                    App.PrintToMiniProfiler(MiniProfilerCategory, "Body", body);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// 拼接 Url 地址
        /// </summary>
        /// <param name="method"></param>
        /// <param name="httpMethodAttribute"></param>
        /// <returns></returns>
        private static string CombineUrlAddress(MethodInfo method, HttpMethodAttribute httpMethodAttribute)
        {
            var urlAddress = httpMethodAttribute.Url;

            // 拼接url地址
            if (!urlAddress.StartsWith("http:") && !urlAddress.StartsWith("https:"))
            {
                // 获取主机地址和端口(需要缓存)
                var hostAttribute = !method.IsDefined(typeof(HostAttribute), true)
                    ? (!method.ReflectedType.IsDefined(typeof(HostAttribute), true)
                        ? default
                        : method.ReflectedType.GetCustomAttribute <HostAttribute>(true))
                    : method.GetCustomAttribute <HostAttribute>(true);

                if (hostAttribute != null && !string.IsNullOrEmpty(hostAttribute.BaseAddress))
                {
                    urlAddress = hostAttribute.BaseAddress
                                 + (hostAttribute.Port == 0 ? string.Empty : $":{hostAttribute.Port}")
                                 + Regex.Replace("/" + urlAddress, @"\/{2,}", "/");
                }
            }

            return(urlAddress);
        }