コード例 #1
0
        protected void Application_Error(object sender, EventArgs e)
        {
            ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

            HttpApplication ap = sender as HttpApplication;

            System.Exception ex = ap.Server.GetLastError();

            var httpStatusCode = (ex is HttpException) ? (ex as HttpException).GetHttpCode() : 500; //这里仅仅区分两种错误

            switch (httpStatusCode)
            {
            case 404:
                break;

            default:
                log.Error("应用程序捕捉到异常", ex);
                APIResult result = new APIResult(ResultType.Exception, ResultCode.Exception, "程序内容错误");
                ap.Context.Response.Write("fsdffdsf");
                ap.Context.Response.End();
                break;
            }
        }
コード例 #2
0
 public APIResponse ResponseResult(APIResult result)
 {
     return(new APIResponse(result));
 }
コード例 #3
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;
            }
        }