Пример #1
0
        /// <summary>
        /// 设置参数到http请求内容
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="parameter">特性关联的参数</param>
        protected override void SetHttpContent(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            var formatter = context.HttpApiConfig.JsonFormatter;
            var options   = context.HttpApiConfig.FormatOptions.CloneChange(this.DateTimeFormat);
            var content   = formatter.Serialize(parameter.Value, options);

            context.RequestMessage.Content = new StringContent(content, Encoding.UTF8, "application/json");
        }
 /// <summary>
 /// http请求之前
 /// </summary>
 /// <param name="context">上下文</param>
 /// <param name="parameter">特性关联的参数</param>
 /// <returns></returns>
 async Task IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
 {
     if (this.IsIgnoreWith(parameter) == false)
     {
         context.RequestMessage.AddMulitpartText(parameter.Name, parameter.ToString());
         await ApiTask.CompletedTask;
     }
 }
Пример #3
0
 /// <summary>
 /// 设置参数到http请求内容
 /// </summary>
 /// <param name="context">上下文</param>
 /// <param name="parameter">特性关联的参数</param>
 /// <exception cref="HttpApiConfigException"></exception>
 protected virtual void SetHttpContent(ApiActionContext context, ApiParameterDescriptor parameter)
 {
     if (context.RequestMessage.Content != null)
     {
         var message = $"参数{parameter.ParameterType.Name} {parameter.Name}必须置前";
         // throw new HttpApiConfigException(message);
     }
     // context.RequestMessage.Content = parameter.Value as HttpContent;
 }
Пример #4
0
        public Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            if (parameter.Value is HuaweiCloudAccount account)
            {
                context.RequestMessage.Headers.Add("X-WSSE", WsseHelper.BuildWSSEHeader(account.AppKey, account.AppSecret));
            }

            return(Task.CompletedTask);
        }
Пример #5
0
        /// <summary>
        /// 执行请求前
        /// </summary>
        /// <param name="context"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            const string headerName = "Authorization";
            var          header     = context.RequestMessage.Headers;

            header.Remove(headerName);
            header.TryAddWithoutValidation(headerName, this.GetAuthorizationValue());
            return(ApiTask.CompletedTask);
        }
        /// <summary>
        /// 设置参数到http请求内容
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="parameter">特性关联的参数</param>
        /// <exception cref="HttpApiConfigException"></exception>
        /// <returns></returns>
        protected override async Task SetHttpContentAsync(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            var form        = parameter.ToString();
            var httpContent = await UrlEncodedContent.FromHttpContentAsync(context.RequestMessage.Content).ConfigureAwait(false);

            await httpContent.AddRawFormAsync(form).ConfigureAwait(false);

            context.RequestMessage.Content = httpContent;
        }
Пример #7
0
        /// <summary>
        /// 执行前
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override async Task BeforeRequestAsync(ApiActionContext context)
        {
            if (string.IsNullOrEmpty(this.name))
            {
                throw new NotSupportedException("请传入name和value参数:" + this.GetType().Name);
            }

            await context.RequestMessage.AddFieldAsync(this.name, this.value);
        }
Пример #8
0
 /// <summary>
 /// 设置参数到http请求内容
 /// </summary>
 /// <param name="context">上下文</param>
 /// <param name="parameter">特性关联的参数</param>
 /// <exception cref="HttpApiConfigException"></exception>
 protected virtual void SetHttpContent(ApiActionContext context, ApiParameterDescriptor parameter)
 {
     if (context.RequestMessage.Content != null)
     {
         var message = string.Format("参数{0} {1}必须置前", parameter.ParameterType.Name, parameter.Name);
         throw new HttpApiConfigException(message);
     }
     context.RequestMessage.Content = parameter.Value as HttpContent;
 }
Пример #9
0
        /// <summary>
        /// 执行请求前
        /// </summary>
        /// <param name="context"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        Task IApiParameterable.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            var formatter = context.HttpApiConfig.JsonFormatter;
            var options   = context.HttpApiConfig.FormatOptions;
            var json      = formatter.Serialize(this.oprations, options);

            context.RequestMessage.Content = new JsonPatchContent(json, Encoding.UTF8);
            return(ApiTask.CompletedTask);
        }
Пример #10
0
        public Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            if (parameter.Value is string authStr)
            {
                context.RequestMessage.Headers.TryAddWithoutValidation("Authorization", authStr);
            }

            return(Task.CompletedTask);
        }
Пример #11
0
        /// <summary>
        /// 准备请求之前
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        async Task IApiActionFilter.OnBeginRequestAsync(ApiActionContext context)
        {
            using (await this.asyncRoot.LockAsync().ConfigureAwait(false))
            {
                await this.InitOrRefreshTokenAsync().ConfigureAwait(false);
            }

            this.AccessTokenResult(context, this.token);
        }
        /// <summary>
        /// 执行前
        /// </summary>
        /// <param name="context">上下文</param>
        /// <exception cref="HttpApiConfigException"></exception>
        /// <returns></returns>
        public override void BeforeRequestAsync(ApiActionContext context)
        {
            var baseUri    = context.RequestMessage.RequestUri;
            var relative   = string.IsNullOrEmpty(this.Path) ? null : new Uri(this.Path, UriKind.RelativeOrAbsolute);
            var requestUri = this.GetRequestUri(baseUri, relative);

            context.RequestMessage.Method     = this.Method;
            context.RequestMessage.RequestUri = requestUri;
        }
Пример #13
0
 /// <summary>
 /// http请求之前
 /// </summary>
 /// <param name="context">上下文</param>
 /// <param name="parameter">特性关联的参数</param>
 /// <returns></returns>
 Task IApiParameterable.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
 {
     if (this.WillIgnore(this.stringValue) == false)
     {
         //context.RequestMessage.AddMulitpartText(parameter.Name, this.stringValue);
         // ApiTask.CompletedTask;
     }
     return(ApiTask.CompletedTask);
 }
Пример #14
0
        /// <summary>
        /// 获取http请求内容
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="parameter">特性关联的参数</param>
        /// <returns></returns>
        protected sealed override HttpContent GetHttpContent(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            var encoding = Encoding.UTF8;
            var q        = from kv in base.FormatParameter(parameter)
                           select string.Format("{0}={1}", kv.Key, HttpUtility.UrlEncode(kv.Value, encoding));

            var content = string.Join("&", q);

            return(new StringContent(content, encoding, "application/x-www-form-urlencoded"));
        }
            /// <summary>
            /// 获取缓存key
            /// </summary>
            /// <param name="context"></param>
            /// <returns></returns>
            public Task <string> GetCacheKeyAsync(ApiActionContext context)
            {
                var method       = context.ApiActionDescriptor.Member;
                var keyGenerator = context.GetService <IEasyCachingKeyGenerator>();
                var arguments    = context.ApiActionDescriptor.Arguments;

                var cacheKey = keyGenerator.GetCacheKey(method, arguments, this.prefix);

                return(Task.FromResult(cacheKey));
            }
Пример #16
0
            /// <summary>
            /// 获取缓存key
            /// </summary>
            /// <param name="context"></param>
            /// <returns></returns>
            public Task <string> GetCacheKeyAsync(ApiActionContext context)
            {
                var method       = context.ApiActionDescriptor.Member;
                var keyGenerator = context.GetService <IEasyCachingKeyGenerator>();
                var parameters   = context.ApiActionDescriptor.Parameters.Select(x => x.Value).ToArray();

                var cacheKey = keyGenerator.GetCacheKey(method, parameters, this.prefix);

                return(Task.FromResult(cacheKey));
            }
 /// <summary>
 /// 执行前
 /// </summary>
 /// <param name="context">上下文</param>
 /// <param name="parameter">参数</param>
 /// <returns></returns>
 public async Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
 {
     if (this.IsIgnoreWith(parameter) == false)
     {
         var options   = context.HttpApiConfig.FormatOptions;
         var json      = context.HttpApiConfig.JsonFormatter.Serialize(parameter.Value, options);
         var fieldName = parameter.Name;
         await context.RequestMessage.AddFormFieldAsync(fieldName, json);
     }
 }
Пример #18
0
        /// <summary>
        /// 查找Html 中的IP地址
        /// </summary>
        protected async override Task <object> GetTaskResult(ApiActionContext context)
        {
            var html = await context.ResponseMessage.Content.ReadAsStringAsync();

            var reg = new Regex(@"(?<=\[).*?(?=\])");       // 只找 [] 里面的内容
            //var reg = new Regex(@"\d+\.\d+\.\d+\.\d+");   // 找到IP 地址
            Match m = reg.Match(html);

            return(m.Value);
        }
Пример #19
0
        /// <summary>
        /// 准备请求之前
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override async Task OnBeginRequestAsync(ApiActionContext context)
        {
            var request = new Request
            {
                Time    = DateTime.Now,
                Message = await context.RequestMessage.ToStringAsync().ConfigureAwait(false)
            };

            context.Tags.Set(tagKey, request);
        }
Пример #20
0
        /// <summary>
        /// http请求之前
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="parameter">特性关联的参数</param>
        /// <returns></returns>
        async Task IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            var stringValue = parameter.Value == null ? null : parameter.Value.ToString();
            var httpContent = context.EnsureNoGet().RequestMessage.Content.CastOrCreateMultipartContent();

            httpContent.AddText(parameter.Name, stringValue);
            context.RequestMessage.Content = httpContent;

            await TaskExtend.CompletedTask;
        }
Пример #21
0
        /// <summary>
        /// 执行前
        /// </summary>
        /// <param name="context">上下文</param>
        /// <exception cref="HttpApiConfigException"></exception>
        /// <returns></returns>
        public override Task BeforeRequestAsync(ApiActionContext context)
        {
            if (this.TimeSpan.HasValue == false)
            {
                throw new HttpApiConfigException($"请传入milliseconds参数:{nameof(TimeoutAttribute)}");
            }

            this.SetTimeout(context, this.TimeSpan.Value);
            return(ApiTask.CompletedTask);
        }
        /// <summary>
        /// 执行前
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override Task BeforeRequestAsync(ApiActionContext context)
        {
            var proxy = new WebProxy(this.host, this.port)
            {
                Credentials = this.credential
            };

            context.HttpApiConfig.HttpClient.SetProxy(proxy);
            return(ApiTask.CompletedTask);
        }
Пример #23
0
        /// <summary>
        /// 设置Cookie值到容器
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="cookieValues">cookie值</param>
        private void SetCookieToContainer(ApiActionContext context, string cookieValues)
        {
            var baseUrl   = context.RequestMessage.RequestUri;
            var container = context.HttpApiConfig.HttpClientHandler.CookieContainer;

            foreach (var cookie in this.GetCookies(cookieValues))
            {
                container.Add(baseUrl, cookie);
            }
        }
Пример #24
0
        /// <summary>
        /// 执行前
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public override async Task BeforeRequestAsync(ApiActionContext context)
        {
            if (string.IsNullOrEmpty(this.name))
            {
                throw new NotSupportedException("请传入name和value参数:" + this.GetType().Name);
            }

            context.RequestMessage.AddMulitpartText(this.name, this.value);
            await ApiTask.CompletedTask;
        }
Пример #25
0
        public Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            if (parameter.Value is AwsV4.SignedResult signer)
            {
                context.RequestMessage.Headers.TryAddWithoutValidation("authorization", signer.Authorization);
                context.RequestMessage.Headers.TryAddWithoutValidation("x-amz-date", signer.Time);
            }

            return(Task.CompletedTask);
        }
        /// <summary>
        /// 请求前
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task OnBeginRequestAsync(ApiActionContext context)
        {
            var request  = context.RequestMessage;
            var dateTime = DateTime.Now.ToString("HH:mm:ss.fff");

            Console.WriteLine("{0} {1} {2}", dateTime, request.Method, request.RequestUri);

            context.Tags.Set("BeginTime", DateTime.Now);
            return(base.OnBeginRequestAsync(context));
        }
Пример #27
0
        /// <summary>
        /// 获取异步结果
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        protected override async Task <object> GetTaskResult(ApiActionContext context)
        {
            var response = context.ResponseMessage;
            var json     = await response.Content.ReadAsStringAsync();

            var dataType = context.ApiActionDescriptor.Return.DataType;
            var result   = context.HttpApiConfig.JsonFormatter.Deserialize(json, dataType);

            return(result);
        }
Пример #28
0
 /// <summary>
 /// 执行前
 /// </summary>
 /// <param name="context">上下文</param>
 /// <returns></returns>
 public override void BeforeRequestAsync(ApiActionContext context)
 {
     if (string.IsNullOrEmpty(this.name))
     {
         throw new NotSupportedException("请传入name和value参数:" + this.GetType().Name);
     }
     if (this.WillIgnore(this.value) == false)
     {
         context.RequestMessage.AddFormFieldAsync(this.name, this.value);
     }
 }
Пример #29
0
        public Task SendAsync(ApiActionContext context)
        {
            if (context is SocketApiActionContext socketContext)
            {
                MessageSendHandler  handler  = RpcServerLoader.Instance.GetMessageSendHandler();
                MessageSendCallBack callBack = handler.SendRequest(socketContext.RequestMessage);
                socketContext.ResponseMessage = callBack.Start();
            }

            return(Task.CompletedTask);
        }
Пример #30
0
        /// <summary>
        /// 获取http请求内容
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="parameter">特性关联的参数</param>
        /// <returns></returns>
        protected override HttpContent GetHttpContent(ApiActionContext context, ApiParameterDescriptor parameter)
        {
            var xmlSerializer = new XmlSerializer(parameter.ParameterType);

            using (var stream = new MemoryStream())
            {
                xmlSerializer.Serialize(stream, parameter.Value);
                var xml = Encoding.UTF8.GetString(stream.ToArray());
                return(new StringContent(xml, Encoding.UTF8, "application/xml"));
            }
        }