コード例 #1
0
        private async Task <string> GetResponseContentAsync(HttpActionExecutedContext actionContext, DateTime responseTime)
        {
            string content = await actionContext.Response.Content.ReadAsStringAsync();

            MonitorApiLog monitorApiLog = actionContext.ActionContext.ActionArguments[key] as MonitorApiLog;

            monitorApiLog.ResponseTime = responseTime;
            monitorApiLog.ResponseData = content;//form表单提交的数据
            Log.Info(string.Format("API响应:{0}", monitorApiLog.ToString()));
            return(content);
        }
コード例 #2
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            try
            {
                SetTrackID();
                Log.Info("调用API接口");
                DateTime requestTime   = DateTime.Now;
                var      request       = ((HttpContextWrapper)actionContext.Request.Properties["MS_HttpContext"]).Request;
                var      requestMethod = request.HttpMethod;

                string app_key         = request.Headers["key"];
                string app_sign        = request.Headers["sign"];
                string app_timestamp_s = request.Headers["timestamp"];
                string app_data        = null;
                if (requestMethod == "POST")
                {
                    Stream stream = HttpContext.Current.Request.InputStream;
                    stream.Seek(0, SeekOrigin.Begin);
                    app_data = new StreamReader(stream).ReadToEnd();


                    #region 过滤图片
                    if (app_data.LastIndexOf(",\"ImgData\":{") > -1)
                    {
                        //Log.Info("去掉图片之前的数据:" + app_data);
                        int x = app_data.LastIndexOf(",\"ImgData\":{");
                        app_data  = app_data.Substring(0, x);
                        app_data += "}";
                        Log.Info("去掉图片之后的数据:" + app_data);
                    }
                    else if (app_data.LastIndexOf(",\"imgData\":{") > -1)
                    {
                        // Log.Info("去掉图片之前的数据:" + app_data);
                        int x = app_data.LastIndexOf(",\"imgData\":{");
                        app_data  = app_data.Substring(0, x);
                        app_data += "}";
                        Log.Info("去掉图片之后的数据:" + app_data);
                    }

                    #endregion
                }
                else
                {
                    NameValueCollection         queryForm = HttpContext.Current.Request.QueryString;
                    Dictionary <string, string> queryData = new Dictionary <string, string>();
                    for (int f = 0; f < queryForm.Count; f++)
                    {
                        string querykey = queryForm.Keys[f];
                        queryData.Add(querykey, queryForm[querykey]);
                    }
                    app_data = GetQueryData(queryData);
                }

                //记录请求的日志
                MonitorApiLog monitorApiLog = new MonitorApiLog();
                monitorApiLog.RequestTime   = requestTime;
                monitorApiLog.RequestUrl    = request.RawUrl;
                monitorApiLog.SignatureData = new SignatureData {
                    Key = app_key, Sign = app_sign, TimeStamp = app_timestamp_s, Data = app_data
                };
                Log.Info(string.Format("API请求:{0}", monitorApiLog.ToString()));

                actionContext.ActionArguments[key] = monitorApiLog;

                //检查必要的参数
                if (app_key == null || app_sign == null || app_timestamp_s == null)
                {
                    APIResult result = new APIResult(ResultType.Failure, ResultCode.FailureSign, "缺少必要参数");
                    actionContext.Response = new APIResponse(result);
                    return;
                }

                //检查key是否在数据库中存在
                string app_secret = SysFactory.AppKeySecret.GetSecret(app_key);

                if (app_secret == null)
                {
                    APIResult result = new APIResult(ResultType.Failure, ResultCode.FailureSign, "应用程序Key,存在错误");
                    actionContext.Response = new APIResponse(result);
                    return;
                }

                long app_timestamp = long.Parse(app_timestamp_s);

                string signStr = Signature.Compute(app_key, app_secret, app_timestamp, app_data);

                Log.Info("app_key:" + app_key);
                Log.Info("app_secret:" + app_secret);
                Log.Info("app_timestamp:" + app_timestamp);
                Log.Info("app_data:" + app_data);
                Log.Info("signStr:" + signStr);
                Log.Info("app_sign:" + app_sign);


                if (Signature.IsRequestTimeout(app_timestamp))
                {
                    APIResult result = new APIResult(ResultType.Failure, ResultCode.FailureSign, "请求已超时");
                    actionContext.Response = new APIResponse(result);
                    return;
                }



                if (signStr != app_sign)
                {
                    Log.Warn("API签名错误");
                    APIResult result = new APIResult(ResultType.Failure, ResultCode.FailureSign, "签名错误");
                    actionContext.Response = new APIResponse(result);
                    return;
                }


                base.OnActionExecuting(actionContext);
            }
            catch (Exception ex)
            {
                APIResult result = new APIResult(ResultType.Exception, ResultCode.Exception, "内部错误");
                actionContext.Response = new APIResponse(result);
                Log.Error(string.Format("API错误:{0}", ex.Message), ex);
                return;
            }
        }