有时候为了提升WebAPI的性能,减少响应时间,我们会使用压缩和解压,而此刻大大都客户端浏览器都供给了内置的解压撑持。在WebAPI请求的资源越大时,使用压缩对性能提升的效果越明显,而当请求的资源很小时则不需要使用压缩和解压,因为压缩和解压同样也是需要耗费必然的时间的。
看见老外写了一篇ASP.NET Web API GZip compression ActionFilter with 8 lines of code
说实话被这标题吸引了,8行代码实现GZip压缩过滤器,我就照着他的去实践了一番,发明居然中文呈现乱码。
凭据他的实现方法:
1、下载DotNetZipLib库
2、解压后添加Ionic.Zlib.dll的dll引用
3、新建DeflateCompression特性和GZipCompression特性,分袂代表Deflate压缩和GZip压缩,这两种压缩方法的实现代码很相似
差此外处所就是
actContext.Response.Content.Headers.Add("Content-encoding", "gzip");
actContext.Response.Content.Headers.Add("Content-encoding", "deflate");
和
var compressor = new DeflateStream( output, CompressionMode.Compress, CompressionLevel.BestSpeed) var compressor = new GZipStream( output, CompressionMode.Compress, CompressionLevel.BestSpeed)using System.Net.Http; using System.Web.Http.Filters; namespace WebAPI.Filter { public class GZipCompressionAttribute : ActionFilterAttribute { 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] : CompressionHelper.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); } } } using System.Net.Http; using System.Web.Http.Filters; namespace WebAPI.Filter { public class DeflateCompressionAttribute : ActionFilterAttribute { 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] : CompressionHelper.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); } }
4、添加一个压缩辅佐类CompressionHelper
using System.IO; using Ionic.Zlib; namespace WebAPI.Filter { public class CompressionHelper { public static byte[] DeflateByte(byte[] str) { if (str == null) { return null; } using (var output = new MemoryStream()) { using ( var compressor = new DeflateStream( output, CompressionMode.Compress, CompressionLevel.BestSpeed)) { compressor.Write(str, 0, str.Length); } return output.ToArray(); } } public static byte[] GZipByte(byte[] str) { if (str == null) { return null; } using (var output = new MemoryStream()) { using ( var compressor = new GZipStream( output, CompressionMode.Compress, CompressionLevel.BestSpeed)) { compressor.Write(str, 0, str.Length); } return output.ToArray(); } } } }
5、控制器挪用,这里我写的测试代码:
public class TestController : ApiController { StringBuilder sb = new StringBuilder(); [GZipCompression] public string Get(int id) { for (int i = 0; i < 1000;i++ ) { sb.Append("这里是中国的领土" + i); } return sb.ToString() + DateTime.Now.ToLocalTime() + "," + id; } }
先看下不使用压缩,注释//[GZipCompression] 符号,文件巨细是26.4kb,请求时间是1.27s