有時候為了提升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
使用[GZipCompression]标記,添加壓縮後,檔案大小是2.4kb,響應時間是1.21,Respouse Body明顯小了很多,但是響應時間少得并不明顯,因為在本地環境下載下傳太快了,而壓縮解壓卻要消耗一定的時間,界面加載的時間主要消耗在onload上了。有個問題:中文顯示亂碼了。
使用.net自帶的壓縮,在System.IO.Compression中提供了對應的類庫——GZipStream與DeflateStream。控制器調用代碼不變,建立一個CompressContentAttribute.cs類,代碼如下:
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace WebAPI.Filter
{
// <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");
}
}
/// <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");
}
}
}
View Code
運作檢視結果,壓縮能力比DotNetZipLib略差,但是不再出現亂碼了。
把控制器代碼中的标記改為 [DeflateCompression],使用Deflate壓縮再來看下效果:
Deflate壓縮後,Content-Length值為2538,而GZip壓縮Content-Length值為2556,可見Deflate壓縮效果更好。
這裡,WebAPI的壓縮我都是通過Action過濾器的方式來實作,當然你也可以寫在WebAPI中的全局配置中,考慮到有些API接口并不需要使用到壓縮,是以就通過Action過濾器的方式來實作了。
dudu的這篇文章HttpClient與APS.NET Web API:請求内容的壓縮與解壓在用戶端壓縮、在服務端解壓。
部落格位址: | http://www.cnblogs.com/jiekzou/ |
部落格版權: | 本文以學習、研究和分享為主,歡迎轉載,但必須在文章頁面明顯位置給出原文連接配接。 如果文中有不妥或者錯誤的地方還望高手的你指出,以免誤人子弟。如果覺得本文對你有所幫助不如【推薦】一下!如果你有更好的建議,不如留言一起讨論,共同進步! 再次感謝您耐心的讀完本篇文章。 |
其它: | .net-QQ群4:612347965 java-QQ群:805741535 H5-QQ群:773766020 我的拙作《ASP.NET MVC企業級實戰》《H5+移動應用實戰開發》 《Vue.js 2.x實踐指南》 《JavaScript實用教程 》 《Node+MongoDB+React 項目實戰開發》 已經出版,希望大家多多支援! |