// 从响应流中读取响应为实体 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(); } } }