Пример #1
0
        private static object Fun(string method, params object[] sendData)
        {
            var mi = _type.GetMethod(method);

            //方法不存在直接返回null
            if (mi == null)
            {
                return(null);
            }
            //暂存的xml参数组
            var sendDataXml = sendData.Select(CoreConvert.ObjToJsonData).ToList();
            //具体的参数数组声明
            var objArr = new object[sendDataXml.Count];
            //获取参数组
            var ps = mi.GetParameters();

            for (var i = 0; i < ps.Length; i++)
            {
                //根据参数组中的类型反序列化成需要的类型
                objArr[i] = CoreConvert.JsonDataToObj(sendDataXml[i], ps[i].ParameterType);
            }
            var obj = Activator.CreateInstance(_type);

            //执行方法
            return(mi.Invoke(obj, objArr));
        }
Пример #2
0
        /// <summary>
        /// 发送请求(返回有构造函数的类)
        /// </summary>
        /// <param name="method">请求的方法</param>
        /// <param name="sendData">请求参数 (可变参数与服务端参数一致)</param>
        /// <returns></returns>
        public T Send <T>(string method, params object[] sendData) where T : new()
        {
            var ret = SendCore(method, sendData);

            return((T)CoreConvert.JsonDataToObj(ret, typeof(T)));
        }
Пример #3
0
 /// <summary>
 /// 处理请求
 /// </summary>
 /// <param name="request"></param>
 /// <param name="response"></param>
 /// <returns></returns>
 private string HandleRequest(HttpListenerRequest request, HttpListenerResponse response)
 {
     if (request.HttpMethod.ToUpper() != "POST")
     {
         CoreLog.Error("只支持POST提交");
         return(null);
     }
     if (request.InputStream != null)
     {
         string funName = null;
         //具体要执行的方法
         MethodInfo mi = null;
         //参数
         //这里是有客户端发送的正文本数据流的请求
         try
         {
             var byteList = new List <byte>();
             var byteArr  = new byte[2048];
             int readLen;
             var len = 0;
             //接收客户端传过来的数据并转成字符串类型
             do
             {
                 readLen = request.InputStream.Read(byteArr, 0, byteArr.Length);
                 len    += readLen;
                 byteList.AddRange(byteArr);
             } while (readLen != 0);
             //获取得到数据data
             var data = Encoding.UTF8.GetString(byteList.ToArray(), 0, len);
             //判断是否是WebService模式调用
             object[] objArr;
             //url路径
             var postSegments = request.Url.Segments;
             //根据url路径确定方法名
             if (postSegments.Length < 2)
             {
                 response.StatusDescription = "404";
                 response.StatusCode        = 404;
                 CoreLog.Error("找不到服务方法");
                 return(null);
             }
             funName = postSegments[1].TrimEnd('/');
             //判断是否是WebSrviceSoap模式调用
             if (funName == "WebSrviceSoap")
             {
                 if (string.IsNullOrEmpty(data))
                 {
                     response.StatusDescription = "404";
                     response.StatusCode        = 404;
                     CoreLog.Error("找不到服务方法");
                     return(null);
                 }
                 var sendDatas = (SendData)CoreConvert.JsonDataToObj(data, typeof(SendData));
                 //获取方法名相同的所有方法
                 var mis = _serviceFunArr.Where(f =>
                                                string.Equals(f.Name, sendDatas.Method, StringComparison.CurrentCultureIgnoreCase)).ToList();
                 if (mis.Count == 0)
                 {
                     response.StatusDescription = "404";
                     response.StatusCode        = 404;
                     CoreLog.Error("找不到服务方法");
                     return(null);
                 }
                 //获取参数
                 var dataList = (List <string>)CoreConvert.JsonDataToObj(sendDatas.Datas, typeof(List <string>));
                 //这里为空则没有参数
                 if (dataList == null)
                 {
                     foreach (var m in mis)
                     {
                         var ps = m.GetParameters();
                         if (ps.Length != 0)
                         {
                             continue;
                         }
                         //找到没有参数的方法
                         mi = m;
                         break;
                     }
                     //参数默认值null
                     objArr = null;
                 }
                 else
                 {
                     //这里是参数数组
                     objArr = new object[dataList.Count];
                     foreach (var m in mis)
                     {
                         var ps = m.GetParameters();
                         if (ps.Length != dataList.Count)
                         {
                             continue;
                         }
                         //找到参数数量对应的方法
                         mi = m;
                         for (var i = 0; i < ps.Length; i++)
                         {
                             objArr[i] = CoreConvert.JsonDataToObj(dataList[i], ps[i].ParameterType);
                         }
                     }
                 }
             }
             else
             {
                 //获取方法名相同的所有方法
                 var mis = _serviceFunArr.Where(f => string.Equals(f.Name, funName, StringComparison.CurrentCultureIgnoreCase)).ToList();
                 if (mis.Count == 0)
                 {
                     response.StatusDescription = "404";
                     response.StatusCode        = 404;
                     CoreLog.Error("找不到服务方法");
                     return(null);
                 }
                 //这里为空则没有参数
                 if (string.IsNullOrEmpty(data))
                 {
                     foreach (var m in mis)
                     {
                         var ps = m.GetParameters();
                         if (ps.Length != 0)
                         {
                             continue;
                         }
                         //找到没有参数的方法
                         mi = m;
                         break;
                     }
                     //参数默认值null
                     objArr = null;
                 }
                 else
                 {
                     //获取参数
                     var dataArr = data.Split('&');
                     //这里是参数数组
                     objArr = new object[dataArr.Length];
                     foreach (var m in mis)
                     {
                         var ps = m.GetParameters();
                         if (ps.Length != dataArr.Length)
                         {
                             continue;
                         }
                         //找到参数数量对应的方法
                         mi = m;
                         for (var i = 0; i < ps.Length; i++)
                         {
                             foreach (var d in dataArr)
                             {
                                 //拆分参数字符串 获得方法名和值
                                 try
                                 {
                                     var dArr = d.Split('=');
                                     if (ps[i].Name != dArr[0])
                                     {
                                         continue;
                                     }
                                     objArr[i] = dArr[1];
                                     break;
                                 }
                                 catch (Exception e)
                                 {
                                     CoreLog.Error("参数错误:" + e);
                                     response.StatusDescription = "404";
                                     response.StatusCode        = 404;
                                     return(null);
                                 }
                             }
                         }
                     }
                 }
             }
             return(Fun(mi, objArr));
         }
         catch (Exception ex)
         {
             response.StatusDescription = "404";
             response.StatusCode        = 404;
             CoreLog.Error($"接收数据时发生错误 Url:{request.Url} Method:{funName} err:{ex}");
             return(null);
         }
     }
     response.StatusDescription = "404";
     response.StatusCode        = 404;
     CoreLog.Error("不允许空提交");
     return(null);
 }