因为不能直接访问指定数据库,只能经过跳板机查询Oracle数据,因此要作一个数据中转接口,算法
查询数据就要压缩,因而就找资料,代码以下,其中要注意的是Response.Headers.Remove("Content-Encoding"); 数据库
这段,对Response.Headrs的操做若是IIS6是不支持的,json
会出现以下错误:app
接口出现错误:"此操做要求使用 IIS 集成管线模式。" ()
System.PlatformNotSupportedException: 此操做要求使用 IIS 集成管线模式ide
其实在OnActionExecuting 这个方法中,请求中Content-Encoding原本就是空的,能够没必要操做,this
在项目中添加下面的类;spa
而后在Contoller方法上加上对应数据,便可实现对数据的压缩。code
// <summary> /// 自动识别客户端是否支持压缩,若是支持则返回压缩后的数据 /// Attribute that can be added to controller methods to force content /// to be GZip encoded if the client supports it /// </summary> public class CompressContentAttribute : ActionFilterAttribute { /// <summary> /// Override to compress the content that is generated by /// an action method. /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(HttpActionContext filterContext) { GZipEncodePage(); } /// <summary> /// Determines if GZip is supported /// </summary> /// <returns></returns> public static bool IsGZipSupported() { string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"]; if (!string.IsNullOrEmpty(AcceptEncoding) && (AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate"))) return true; return false; } /// <summary> /// Sets up the current page or handler to use GZip through a Response.Filter /// IMPORTANT: /// You have to call this method before any output is generated! /// </summary> public static void GZipEncodePage() { HttpResponse Response = HttpContext.Current.Response; if (IsGZipSupported()) { string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"]; if (AcceptEncoding.Contains("deflate")) { Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter, System.IO.Compression.CompressionMode.Compress); #region II6不支持此方法,(实际上此值默认为null 也不须要移除) //Response.Headers.Remove("Content-Encoding"); #endregion Response.AppendHeader("Content-Encoding", "deflate"); } else { Response.Filter = new System.IO.Compression.GZipStream(Response.Filter, System.IO.Compression.CompressionMode.Compress); #region II6不支持此方法,(实际上此值默认为null 也不须要移除) //Response.Headers.Remove("Content-Encoding"); #endregion Response.AppendHeader("Content-Encoding", "gzip"); } } // Allow proxy servers to cache encoded and unencoded versions separately Response.AppendHeader("Vary", "Content-Encoding"); } } /// <summary> /// 强制Defalte压缩 /// Content-encoding:gzip,Content-Type:application/json /// DEFLATE是一个无专利的压缩算法,它能够实现无损数据压缩,有众多开源的实现算法。 /// </summary> public class DeflateCompressionAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext filterContext) { HttpResponse Response = HttpContext.Current.Response; Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter, System.IO.Compression.CompressionMode.Compress); #region II6不支持此方法,(实际上此值默认为null 也不须要移除) //Response.Headers.Remove("Content-Encoding"); #endregion Response.AppendHeader("Content-Encoding", "deflate"); } //public override void OnActionExecuted(HttpActionExecutedContext actContext) //{ // var content = actContext.Response.Content; // var bytes = content == null ? null : content.ReadAsByteArrayAsync().Result; // var zlibbedContent = bytes == null ? new byte[0] : // APP.Core.Utility.ZHelper.DeflateByte(bytes); // actContext.Response.Content = new ByteArrayContent(zlibbedContent); // actContext.Response.Content.Headers.Remove("Content-Type"); // actContext.Response.Content.Headers.Add("Content-encoding", "deflate"); // actContext.Response.Content.Headers.Add("Content-Type", "application/json"); // base.OnActionExecuted(actContext); //} } /// <summary> /// 强制GZip压缩,application/json /// Content-encoding:gzip,Content-Type:application/json /// GZIP是使用DEFLATE进行压缩数据的另外一个压缩库 /// </summary> public class GZipCompressionAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext filterContext) { HttpResponse Response = HttpContext.Current.Response; Response.Filter = new System.IO.Compression.GZipStream(Response.Filter, System.IO.Compression.CompressionMode.Compress); #region II6不支持此方法,(实际上此值默认为null 也不须要移除) //Response.Headers.Remove("Content-Encoding"); #endregion Response.AppendHeader("Content-Encoding", "gzip"); } //public override void OnActionExecuted(HttpActionExecutedContext actContext) //{ // var content = actContext.Response.Content; // var bytes = content == null ? null : content.ReadAsByteArrayAsync().Result; // var zlibbedContent = bytes == null ? new byte[0] : // APP.Core.Utility.ZHelper.GZipByte(bytes); // actContext.Response.Content = new ByteArrayContent(zlibbedContent); // actContext.Response.Content.Headers.Remove("Content-Type"); // actContext.Response.Content.Headers.Add("Content-encoding", "gzip"); // actContext.Response.Content.Headers.Add("Content-Type", "application/json"); // base.OnActionExecuted(actContext); //} }
使用方法orm
[DeflateCompression] public string GetDeflate() { return "valuevaluevaluevaluevaluevaluevaluevaluevaluevalueSDFASDFASDFASDFASDFWERFWEFCASDFSDAFASDFASDFASDFSADFSA"; } [CompressContent] public string GetGZip() { return "valuevaluevaluevaluevaluevaluevaluevaluevaluevalueSDFASDFASDFASDFASDFWERFWEFCASDFSDAFASDFASDFASDFSADFSA"; } [GZipCompression] public string GetRaw() { return "valuevaluevaluevaluevaluevaluevaluevaluevaluevalueSDFASDFASDFASDFASDFWERFWEFCASDFSDAFASDFASDFASDFSADFSA"; }