Ejemplo n.º 1
0
 public static SessionInfo GetSessionByToken(string token)
 {
     if (string.IsNullOrEmpty(token))
     {
         return(null);
     }
     return(AppCache.GetCache <SessionInfo>(token));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 实时接口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="interfaceCode"></param>
        /// <param name="interfaceName"></param>
        /// <param name="PostObj"></param>
        /// <returns></returns>
        public static IRestResponse <T> PostExcute <T>(DataRow InterfaceURLRow, string interfaceName, object PostObj) where T : class, new()
        {
            //BDOs tmpInterFaceConfig = new BDOs("SYS_InterfaceURL", "INTERFACECODE = '"+ interfaceCode+"'");
            if (InterfaceURLRow == null)
            {
                throw new ApplicationException("接口地址未配置!");
            }
            string InterfaceURL = InterfaceURLRow["URL"].ToString();

            if (string.IsNullOrEmpty(InterfaceURL))
            {
                throw new ApplicationException("接口地址未配置!");
            }


            string interfaceCode = InterfaceURLRow["INTERFACECODE"].ToString();

            /////////////////////////////////
            //using (var tmpClient = new HttpClient())
            //{
            //    try
            //    {
            //        var str = JsonConvert.SerializeObject(PostObj);
            //        str = str.Replace("STD_IN_Origin", "STD_IN Origin").Replace("Service_Name", "Service Name");
            //        HttpContent tmpContent = new StringContent(str);
            //        tmpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            //        HttpResponseMessage response = tmpClient.PostAsync(InterfaceURL, tmpContent).Result;//改成自己的
            //        response.EnsureSuccessStatusCode();//用来抛异常的
            //        string  tmpResponseBody= response.Content.ReadAsStringAsync().Result;

            //    }
            //    catch (Exception e)
            //    {
            //        string msg = e.Message;
            //    }
            //}
            ///////////////////////////////////

            var str = JsonConvert.SerializeObject(PostObj);

            str = str.Replace("STD_IN_Origin", "STD_IN Origin").Replace("Service_Name", "Service Name");

            var client  = new RestClient(InterfaceURL);
            var request = new RestRequest();

            request.Method = Method.POST;
            request.AddHeader("Accept", "application/json");
            request.Parameters.Clear();
            request.AddParameter("application/json", str, ParameterType.RequestBody);

            //var content = response.Content; // 返回的网页内容

            //var client = new RestClient(InterfaceURL);
            //var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json };
            //request.AddJsonBody(PostObj);
            //request.AddHeader("Content-Type", "application/json; charset=utf-8");

            var content = client.Execute <T>(request);


            //string requestBody = JsonConvert.SerializeObject(PostObj);
            string requestBody = str;

            requestBody = requestBody.Replace("\\\\", "\\");
            string responseBody = content.Content;

            StackTrace trace     = new StackTrace();
            StackFrame frame     = trace.GetFrame(1);
            MethodBase method    = frame.GetMethod();
            String     className = method.ReflectedType.Name;


            if (content.StatusCode != System.Net.HttpStatusCode.OK)
            {
                var  hi    = AppCache.GetCache <RequestHeaderInfo>(Thread.CurrentThread.ManagedThreadId);
                Task task1 = new Task(() =>
                {
                    var ai = new RequestHeaderInfo(hi.LangNo, hi.DBNo, hi.MenuNo, hi.UserNo, hi.UserName, "");
                    AppCache.Add(Thread.CurrentThread.ManagedThreadId, ai);

                    BDO bd1                   = new BDO(null, null, "SYS_InterfaceLog", "1<>1");
                    DataRow drBat1            = bd1.NewRow();
                    drBat1["IsIn"]            = 1;
                    drBat1["SerialNo"]        = Guid.NewGuid();
                    drBat1["FunctionName"]    = method.Name;
                    drBat1["InterfaceName"]   = interfaceName;
                    drBat1["interfaceCode"]   = interfaceCode;
                    drBat1["RequestBody"]     = requestBody;
                    drBat1["ResponseBody"]    = responseBody;
                    drBat1["ResponseCode"]    = "E";
                    drBat1["ResponseMessage"] = content.Content;
                    drBat1["CREATETIME"]      = DateTime.Now;
                    bd1.Rows.Add(drBat1);
                    DataRowsSaveResult drsr1 = bd1.SaveToDB();
                    if (!drsr1.Success)
                    {
                        throw new Exception(drsr1.ErrorText);
                    }
                });
                task1.Start();
                task1.Wait();
            }
            else
            {
                var ResponseCode    = string.Empty;
                var ResponseMessage = string.Empty;
                var ZGUID           = string.Empty;
                try
                {
                    //responseBody=responseBody.Replace("单别-单号", "Message").Replace("错误原因", "Message").Replace("处理结果", "Message");

                    //MES2ERPResult model = Newtonsoft.Json.JsonConvert.DeserializeObject<MES2ERPResult>(responseBody);
                    //if (model == null)
                    //    throw new ApplicationException("返回信息解析失败:" + InterfaceURL + "->" + responseBody);
                    if (content.Data == null)
                    {
                        content.Data = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(responseBody);
                    }
                }
                catch (Exception ex)
                {
                    ResponseCode    = "N/A";
                    ResponseMessage = string.IsNullOrEmpty(responseBody) ? ex.Message : responseBody;
                }

                var  hi    = AppCache.GetCache <RequestHeaderInfo>(Thread.CurrentThread.ManagedThreadId);
                Task task1 = new Task(() =>
                {
                    var ai = new RequestHeaderInfo(hi.LangNo, hi.DBNo, hi.MenuNo, hi.UserNo, hi.UserName, "");
                    AppCache.Add(Thread.CurrentThread.ManagedThreadId, ai);

                    BDO bd1                   = new BDO(null, null, "SYS_InterfaceLog", "1<>1");
                    DataRow drBat1            = bd1.NewRow();
                    drBat1["IsIn"]            = 1;
                    drBat1["SerialNo"]        = Guid.NewGuid();
                    drBat1["FunctionName"]    = method.Name;
                    drBat1["InterfaceName"]   = interfaceName;
                    drBat1["interfaceCode"]   = interfaceCode;
                    drBat1["RequestBody"]     = requestBody;
                    drBat1["ResponseBody"]    = responseBody;
                    drBat1["ResponseCode"]    = ResponseCode;
                    drBat1["ResponseMessage"] = string.IsNullOrEmpty(ResponseMessage) ? "" : ResponseMessage;
                    drBat1["CREATETIME"]      = DateTime.Now;
                    bd1.Rows.Add(drBat1);
                    DataRowsSaveResult drsr1 = bd1.SaveToDB();
                    if (!drsr1.Success)
                    {
                        throw new Exception(drsr1.ErrorText);
                    }
                });
                task1.Start();
                task1.Wait();
            }

            return(content);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 实时接口 POST请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="interfaceCode"></param>
        /// <param name="interfaceName"></param>
        /// <param name="PostObj"></param>
        /// <returns></returns>
        public static IRestResponse <T> PostExcute <T>(DataRow InterfaceURLRow, string interfaceName, object PostObj) where T : class, new()
        {
            //BDOs tmpInterFaceConfig = new BDOs("SYS_InterfaceURL", "INTERFACECODE = '"+ interfaceCode+"'");
            if (InterfaceURLRow == null)
            {
                throw new ApplicationException("接口地址未配置!");
            }
            string InterfaceURL = InterfaceURLRow["URL"].ToString();

            if (string.IsNullOrEmpty(InterfaceURL))
            {
                throw new ApplicationException("接口地址未配置!");
            }


            string interfaceCode = InterfaceURLRow["INTERFACECODE"].ToString();
            var    client        = new RestClient(InterfaceURL);
            var    request       = new RestRequest(Method.POST)
            {
                RequestFormat = DataFormat.Json
            };

            request.AddJsonBody(PostObj);
            request.AddHeader("Content-Type", "application/json; charset=utf-8");

            var content = client.Execute <T>(request);


            string requestBody = JsonConvert.SerializeObject(PostObj);

            requestBody = requestBody.Replace("\\\\", "\\");
            string responseBody = content.Content;

            StackTrace trace  = new StackTrace();
            StackFrame frame  = trace.GetFrame(1);
            MethodBase method = frame.GetMethod();

            //String className = method.ReflectedType.Name;


            if (content.StatusCode != System.Net.HttpStatusCode.OK)
            {
                var  hi    = AppCache.GetCache <RequestHeaderInfo>(Thread.CurrentThread.ManagedThreadId);
                Task task1 = new Task(() =>
                {
                    var ai = new RequestHeaderInfo(hi.LangNo, hi.DBNo, hi.MenuNo, hi.UserNo, hi.UserName, "");
                    AppCache.Add(Thread.CurrentThread.ManagedThreadId, ai);

                    BDO bd1                   = new BDO(null, null, "SYS_InterfaceLog", "1<>1");
                    DataRow drBat1            = bd1.NewRow();
                    drBat1["IsIn"]            = 1;
                    drBat1["SerialNo"]        = Guid.NewGuid();
                    drBat1["FunctionName"]    = method.Name;
                    drBat1["InterfaceName"]   = interfaceName;
                    drBat1["interfaceCode"]   = interfaceCode;
                    drBat1["RequestBody"]     = requestBody;
                    drBat1["ResponseBody"]    = responseBody;
                    drBat1["ResponseCode"]    = "E";
                    drBat1["ResponseMessage"] = content.Content;
                    drBat1["CREATETIME"]      = DateTime.Now;
                    bd1.Rows.Add(drBat1);
                    DataRowsSaveResult drsr1 = bd1.SaveToDB();
                    if (!drsr1.Success)
                    {
                        throw new Exception(drsr1.ErrorText);
                    }
                });
                task1.Start();
                task1.Wait();
            }
            else
            {
                var ResponseCode    = string.Empty;
                var ResponseMessage = string.Empty;
                var ZGUID           = string.Empty;
                try
                {
                    T model = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(responseBody);
                    if (model == null)
                    {
                        throw new ApplicationException("返回信息解析失败:" + InterfaceURL + "->" + responseBody);
                    }
                    if (content.Data == null)
                    {
                        content.Data = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(responseBody);
                    }
                }
                catch (Exception ex)
                {
                    ResponseCode    = "N/A";
                    ResponseMessage = string.IsNullOrEmpty(responseBody) ? ex.Message : responseBody;
                }

                var hi = AppCache.GetCache <RequestHeaderInfo>(Thread.CurrentThread.ManagedThreadId);


                Task task1 = new Task(() =>
                {
                    var ai = new RequestHeaderInfo(hi.LangNo, hi.DBNo, hi.MenuNo, hi.UserNo, hi.UserName, "");
                    AppCache.Add(Thread.CurrentThread.ManagedThreadId, ai);

                    BDO bd1                   = new BDO(null, null, "SYS_InterfaceLog", "1<>1");
                    DataRow drBat1            = bd1.NewRow();
                    drBat1["IsIn"]            = 1;
                    drBat1["SerialNo"]        = Guid.NewGuid();
                    drBat1["FunctionName"]    = method.Name;
                    drBat1["InterfaceName"]   = interfaceName;
                    drBat1["interfaceCode"]   = interfaceCode;
                    drBat1["RequestBody"]     = requestBody;
                    drBat1["ResponseBody"]    = responseBody;
                    drBat1["ResponseCode"]    = ResponseCode;
                    drBat1["ResponseMessage"] = string.IsNullOrEmpty(ResponseMessage) ? "" : ResponseMessage;
                    drBat1["CREATETIME"]      = DateTime.Now;
                    bd1.Rows.Add(drBat1);
                    DataRowsSaveResult drsr1 = bd1.SaveToDB();
                    if (!drsr1.Success)
                    {
                        throw new Exception(drsr1.ErrorText);
                    }
                });
                task1.Start();
                task1.Wait();
            }

            return(content);
        }