예제 #1
0
        public object Execute(object request, Type responseType, string url = null, PostDataFomaterType type = PostDataFomaterType.Json)
        {
            if (string.IsNullOrEmpty(url))
            {
                //生成完整的url
                url = GetRealUrlBase(request, url);
            }
            //访问服务
            string response = PostService(request, url, type);
            object t        = GenerateResponse(response, responseType);

            return(t);
        }
예제 #2
0
        public T Execute <T>(IRequest <T> request, string url = null, PostDataFomaterType type = PostDataFomaterType.Json) where T : BaseResponse
        {
            T t = default(T);

            if (string.IsNullOrEmpty(url))
            {
                //生成完整的url
                url = GetRealUrl(request, url);
            }
            //访问服务
            string response = PostService(request, url, type);

            //通过服务器返回的json生成response对象
            t = GenerateResponse <T>(response);
            return(t);
        }
예제 #3
0
        private string PostService <T>(IRequest <T> request, string fullUrl, PostDataFomaterType type) where T : BaseResponse
        {
            Type requestType = request.GetType();

            PropertyInfo[] properties          = requestType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            Dictionary <string, object> argdic = new Dictionary <string, object>();

            //反射获得请求属性
            foreach (PropertyInfo pro in properties)
            {
                ArgMapping mapping = pro.GetCustomAttributes(typeof(ArgMapping), true).FirstOrDefault() as ArgMapping;
                string     name    = pro.Name;
                if (mapping != null && !string.IsNullOrEmpty(mapping.Mapping))
                {
                    name = mapping.Mapping;
                }
                object value = pro.GetValue(request, null);
                argdic[name] = value;
            }
            //格式化成post数据
            IPostDataFormatter fomatter = PostDataFormatterFactory.Create(type);
            string             json     = fomatter.Format(argdic);

            byte[] data           = Encoding.UTF8.GetBytes(json);
            string zippedResponse = HttpHelper.Post(fullUrl, data);
            //string response = ZipHelper.UnZip(zippedResponse);
            string response = zippedResponse;

            //设置请求信息
            if (request is BaseRequest <T> )
            {
                var req = request as BaseRequest <T>;
                req.Body = new RequestBody
                {
                    PostData = json,
                    URL      = fullUrl,
                };
            }
            return(response);
        }