CutJSONHeader() public static method

将传入的序列化字符串去除头部定义。
public static CutJSONHeader ( string json ) : string
json string 序列化字符串。
return string
Beispiel #1
0
        /// <summary>
        /// 以同步的形式发起HTTP请求并返回类型为泛型的API响应对象。
        /// </summary>
        /// <typeparam name="T">定义结果的泛型。</typeparam>
        /// <returns>返回结果类型为泛型的API响应对象。</returns>
        public virtual APIResponse <T> ToAPIResponse <T>()
        {
            var result = new APIResponse <T>();

            try
            {
                var response = Post().asString();
                result.StatusCode = (HttpStatusCode)response.Code;
                var json = response.Body;

                //判断返回的是否是一个异常数据包
                //如果是的,反序列化之,并引发一个异常
                //反之正常反序列化
                if (APIException.CheckStringIsAPIException(json))
                {
                    result.APIError = JsonConvert.DeserializeObject <APIException>(APIException.CutJSONHeader(json));
                    throw result.APIError.ToException();//这里会引发异常
                }//end if

                if (typeof(T).Equals(typeof(string)))
                {
                    result.Body = (T)(object)json;
                }
                else
                {
                    result.Body = JsonConvert.DeserializeObject <T>(json);
                }
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }

            return(result);
        }//end method
        }//end method

        /// <summary>
        /// 以POST方法发起请求并接收结果类型为泛型的API响应对象。
        /// </summary>
        /// <typeparam name="T">定义结果的泛型。</typeparam>
        /// <returns>返回结果类型为泛型的API响应对象。</returns>
        public virtual APIResponse <T> ToAPIResponse <T>()
        {
            var result = new APIResponse <T>();

            try
            {
                var request = this.HttpUtility.CreateHttpWebRequest();
                this.HttpUtility.WriteHttpWebRequestStream(request.GetRequestStream());
                var response = this.HttpUtility.CreateHttpWebResponse(request);
                result.StatusCode = response.StatusCode;
                var json = this.HttpUtility.GetResponseString(response);

                if (APIException.CheckStringIsAPIException(json))
                {
                    result.APIError = JsonConvert.DeserializeObject <APIException>(APIException.CutJSONHeader(json));
                    throw result.APIError.ToException();
                }//end if

                //判断返回的是否是一个异常数据包
                //如果是的,反序列化之,并引发一个异常
                //反之正常反序列化
                if (APIException.CheckStringIsAPIException(json))
                {
                    result.APIError = JsonConvert.DeserializeObject <APIException>(APIException.CutJSONHeader(json));
                    throw result.APIError.ToException();//这里会引发异常
                }//end if

                if (typeof(T).Equals(typeof(string)))
                {
                    result.Body = (T)(object)json;
                }
                else
                {
                    result.Body = JsonConvert.DeserializeObject <T>(json);
                }
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }

            return(result);
        }//end method