Example #1
0
        // 从响应流中读取响应为实体
        static T ReadAsResult <T>(Stream stream, Encoding encoding = null, Func <string, T> deserializer = null)
        {
            StreamReader reader = null;
            string       json   = string.Empty;

            try
            {
                // TODO 压缩类型流
                reader = encoding != null ? new StreamReader(stream, encoding) : new StreamReader(stream);
                json   = reader.ReadToEnd();
                if (typeof(T) == typeof(string))
                {
                    return((T)(json as object));
                }
                else
                {
                    T value = deserializer != null?deserializer(json) : SerializeHelper.DeserializeFromJson <T>(json);

                    return(value);
                }
            }
            catch (Exception e)
            {
                if (string.IsNullOrEmpty(json))
                {
                    throw;
                }
                else
                {
                    // 抛出返回的原始 JSON
                    WebException we   = e as WebException;
                    string       line = e.Message;
                    if (we != null)
                    {
                        line = WebHelper.ReadWebException(we);
                    }

                    string message = string.Format("{0}{1}{2}", line, Environment.NewLine, json);
                    throw new XFrameworkException(message, e);
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
Example #2
0
        /// <summary>
        /// 取指定KEY值的Cookie
        /// </summary>
        public static T GetCookie <T>(string key, bool decrypt = true)
            where T : class, new()
        {
            string cookieValue = WebHelper.GetCookie(key, decrypt);

            if (string.IsNullOrEmpty(cookieValue))
            {
                return(default(T));
            }

            if (decrypt)
            {
                cookieValue = SecurityHelper.DESDecrypt(cookieValue);
            }
            T TJson = SerializeHelper.DeserializeFromJson <T>(cookieValue);

            return(TJson);
        }