Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="fullUrl"></param>
        /// <param name="type"></param>
        /// <param name="encryptor"></param>
        /// <returns></returns>
        protected string InvokeService(object request, string fullUrl, ContentTypeEnum type, IServiceEncryptor encryptor = null)
        {
            Type requestType = request.GetType();

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

            //反射获得请求属性
            foreach (PropertyInfo pro in properties)
            {
                bool isPostData    = false;
                bool isHeaderValue = false;
                bool isCookieValue = false;
                bool ignore        = false;
                ignore = pro.GetCustomAttributes(typeof(IgnoreAttribute), true).Length > 0;
                if (ignore)
                {
                    continue;
                }
                isPostData    = pro.GetCustomAttributes(typeof(PostDataAttribute), true).Length > 0;
                isHeaderValue = pro.GetCustomAttributes(typeof(HeaderValueAttribute), true).Length > 0;
                isCookieValue = pro.GetCustomAttributes(typeof(CookieValueAttribute), true).Length > 0;
                if (isPostData)
                {
                    object proValue = pro.GetValue(request, null);
                    if (proValue != null)
                    {
                        if (proValue is IEnumerable)
                        {
                            argdic[pro.Name] = proValue;
                        }
                        else if (proValue.GetType().IsValueType)
                        {
                            argdic[pro.Name] = proValue;
                        }
                        else
                        {
                            var proProperties = pro.PropertyType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                            foreach (PropertyInfo p in proProperties)
                            {
                                ArgMapping mapping = p.GetCustomAttributes(typeof(ArgMapping), true).FirstOrDefault() as ArgMapping;
                                string     name    = p.Name;
                                if (mapping != null && !string.IsNullOrEmpty(mapping.Mapping))
                                {
                                    name = mapping.Mapping;
                                }
                                object value = p.GetValue(proValue, null);
                                argdic[name] = value;
                            }
                        }
                    }
                }
                else if (isHeaderValue)
                {
                    object proValue = pro.GetValue(request, null);
                    headers[pro.Name] = proValue?.ToString();
                }
                else if (isCookieValue)
                {
                    object proValue = pro.GetValue(request, null);
                    cookies[pro.Name] = proValue?.ToString();
                }
                else
                {
                    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);
                    if (value != null)
                    {
                        argdic[name] = value;
                    }
                }
            }
            string         response = "";
            var            postdata = "";
            HttpMethodEnum method   = HttpMethodEnum.Post;
            var            getAttrs = requestType.GetCustomAttributes(typeof(GetAttribute), true);
            var            putAttrs = requestType.GetCustomAttributes(typeof(PutAttribute), true);

            if (getAttrs != null && getAttrs.Length > 0)
            {
                method = HttpMethodEnum.Get;
            }
            if (putAttrs != null && putAttrs.Length > 0)
            {
                method = HttpMethodEnum.Put;
            }
            //用于restful,替换例如/{id}等url变量
            foreach (var kv in argdic)
            {
                if (kv.Value.GetType().IsValueType || kv.Value.GetType().Equals(typeof(string)))
                {
                    fullUrl = fullUrl.Replace("{" + kv.Key + "}", kv.Value.ToString());
                }
            }
            switch (method)
            {
            case HttpMethodEnum.Post:
            case HttpMethodEnum.Put:
                //格式化成post数据
                IPostDataFormatter fomatter = PostDataFormatterFactory.Create(type);
                postdata = fomatter.Format(argdic);
                if (encryptor != null)
                {
                    postdata = encryptor.Encrypt(postdata);
                }
                string typeString = ContentTypeConvert.ToTypeString(type);
                byte[] data       = Encoding.UTF8.GetBytes(postdata);
                switch (method)
                {
                case HttpMethodEnum.Post:
                    response = HttpHelper.Post(fullUrl, data, contentType: typeString, header: headers, cookieDic: cookies);
                    break;

                case HttpMethodEnum.Put:
                    response = HttpHelper.Put(fullUrl, data, contentType: typeString, header: headers, cookieDic: cookies);
                    break;
                }
                break;

            case HttpMethodEnum.Get:
                type     = ContentTypeEnum.UrlEncoded;
                postdata = fullUrl;
                response = HttpHelper.Get(fullUrl, argdic, header: headers, cookieDic: cookies);
                break;
            }

            //设置请求信息
            if (request.GetType().IsInheritFrom(typeof(BaseRequest <>)))
            {
                dynamic req = request;
                req.Body = new RequestBody
                {
                    PostData = postdata,
                    URL      = fullUrl,
                };
            }
            return(response);
        }