日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WebAPI性能优化之压缩解压

發(fā)布時間:2025/3/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WebAPI性能优化之压缩解压 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

有時候為了提升WebAPI的性能,減少響應(yīng)時間,我們會使用壓縮和解壓,而現(xiàn)在大多數(shù)客戶端瀏覽器都提供了內(nèi)置的解壓支持。在WebAPI請求的資源越大時,使用壓縮對性能提升的效果越明顯,而當請求的資源很小時則不需要使用壓縮和解壓,因為壓縮和解壓同樣也是需要耗費一定的時間的。

看見老外寫了一篇ASP.NET Web API GZip compression ActionFilter with 8 lines of code

說實話被這標題吸引了,8行代碼實現(xiàn)GZip壓縮過濾器,我就照著他的去實踐了一番,發(fā)現(xiàn)居然中文出現(xiàn)亂碼。

按照他的實現(xiàn)方式:

1、下載DotNetZipLib庫

2、解壓后添加Ionic.Zlib.dll的dll引用

3、新建DeflateCompression特性和GZipCompression特性,分別代表Deflate壓縮和GZip壓縮,這兩種壓縮方式的實現(xiàn)代碼很相似

不同的地方就是

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、控制器調(diào)用,這里我寫的測試代碼:

public class TestController : ApiController{StringBuilder sb = new StringBuilder(); [GZipCompression]public string Get(int id){for (int i = 0; i < 1000;i++ ){sb.Append("這里是中國的領(lǐng)土" + i);}return sb.ToString() + DateTime.Now.ToLocalTime() + "," + id;}}

先看下不使用壓縮,注釋//[GZipCompression] 標記,文件大小是26.4kb,請求時間是1.27s

使用[GZipCompression]標記,添加壓縮后,文件大小是2.4kb,響應(yīng)時間是1.21,Respouse Body明顯小了很多,但是響應(yīng)時間少得并不明顯,因為在本地環(huán)境下載太快了,而壓縮解壓卻要消耗一定的時間,界面加載的時間主要消耗在onload上了。有個問題:中文顯示亂碼了。

?使用.net自帶的壓縮,在System.IO.Compression中提供了對應(yīng)的類庫——GZipStream與DeflateStream。控制器調(diào)用代碼不變,新建一個CompressContentAttribute.cs類,代碼如下:

using System.Web; using System.Web.Http.Controllers; using System.Web.Http.Filters;namespace WebAPI.Filter {// <summary>/// 自動識別客戶端是否支持壓縮,如果支持則返回壓縮后的數(shù)據(jù)/// 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");#endregionResponse.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");#endregionResponse.AppendHeader("Content-Encoding", "gzip");}}// Allow proxy servers to cache encoded and unencoded versions separatelyResponse.AppendHeader("Vary", "Content-Encoding");}}/// <summary>/// 強制Defalte壓縮/// Content-encoding:gzip,Content-Type:application/json/// DEFLATE是一個無專利的壓縮算法,它可以實現(xiàn)無損數(shù)據(jù)壓縮,有眾多開源的實現(xiàn)算法。/// </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");#endregionResponse.AppendHeader("Content-Encoding", "deflate");}}/// <summary>/// 強制GZip壓縮,application/json/// Content-encoding:gzip,Content-Type:application/json/// GZIP是使用DEFLATE進行壓縮數(shù)據(jù)的另一個壓縮庫/// </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");#endregionResponse.AppendHeader("Content-Encoding", "gzip");}} } View Code

運行查看結(jié)果,壓縮能力比DotNetZipLib略差,但是不再出現(xiàn)亂碼了。

把控制器代碼中的標記改為?? [DeflateCompression],使用Deflate壓縮再來看下效果:

?

Deflate壓縮后,Content-Length值為2538,而GZip壓縮Content-Length值為2556,可見Deflate壓縮效果更好。

這里,WebAPI的壓縮我都是通過Action過濾器的方式來實現(xiàn),當然你也可以寫在WebAPI中的全局配置中,考慮到有些API接口并不需要使用到壓縮,所以就通過Action過濾器的方式來實現(xiàn)了。

dudu的這篇文章HttpClient與APS.NET Web API:請求內(nèi)容的壓縮與解壓在客戶端壓縮、在服務(wù)端解壓。

轉(zhuǎn)載于:https://www.cnblogs.com/jiekzou/p/6277429.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的WebAPI性能优化之压缩解压的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 动漫同人高h啪啪爽文 | 色一情一交一乱一区二区三区 | 久久噜噜 | 久操视频精品 | 视频在线看 | 激情综合图| 波多野结衣av片 | 欧美高清在线一区 | 熟妇女人妻丰满少妇中文字幕 | 波多野结衣在线免费观看视频 | 中文字幕av久久爽一区 | 天天高潮夜夜爽 | 波多野结衣av无码 | 韩国伦理在线看 | 久久久18禁一区二区三区精品 | 日批在线观看视频 | 114国产精品久久免费观看 | 91原创视频 | 狠狠干狠狠干狠狠干 | 国产伦理吴梦梦伦理 | 熟妇人妻中文字幕无码老熟妇 | 在线亚洲+欧美+日本专区 | 成人欧美视频 | 亚洲人成人网 | 成人动漫一区二区三区 | 日本高潮视频 | 91丝袜国产在线播放 | 亚洲欧美在线免费观看 | 欧美又粗又大xxxxbbbb疯狂 | 国产欧美一区二区三区免费看 | 欧美久久久久久又粗又大 | 国产精品水嫩水嫩 | 国产视频网站在线观看 | 香港一级淫片免费放 | 精品人妻av一区二区 | 日人视频 | 成人在线视频一区二区三区 | 老妇女玩小男生毛片 | 色噜噜日韩精品欧美一区二区 | www.haoav| 久久国产成人精品国产成人亚洲 | 国产超碰人人 | www色网站 | 日本熟妇乱子伦xxxx | 少妇毛片一区二区三区 | 欧美一级爽aaaaa大片 | 凹凸国产熟女精品视频 | a视频在线 | 精品久久久久久久久久久久久久久久 | 这里只有精品视频在线观看 | 精品国产影院 | 亚洲人成无码网站久久99热国产 | 日韩欧美一区二区三区在线观看 | 天天热天天干 | 精品国产无码一区二区 | 嫩草视频网站 | 中文字幕久热 | 免费国产黄色 | 欧美女优在线 | 国产一区二区三区欧美 | 午夜激情久久久 | 色噜噜色综合 | 男男gay动漫 | 国产成人在线电影 | 麻豆爱爱 | 最新毛片基地 | 欧美日本一区二区三区 | 久久久婷| 日本黄色一区二区三区 | 久久激情视频 | 午夜精品久久久久久久久久久久久 | 999zyz玖玖资源站永久 | 国产九色91回来了 | 成人av播放 | 国产精品99精品无码视 | 91麻豆精品国产午夜天堂 | 久色免费视频 | 少妇日b| 欧美zozo| 精品久久久一区二区 | 激情www| 中文字幕在线播出 | 视频在线日韩 | 国产无遮挡又黄又爽免费视频 | 日欧美女人| 欧美国产第一页 | 国产精品国产三级国产aⅴ浪潮 | 日日噜噜夜夜狠狠久久丁香五月 | 欧美精品一 | 国产福利在线免费观看 | 日韩二区视频 | 成人激情片 | 成人欧美性| 疯狂做爰高潮videossex | 久久网免费视频 | 国产精品天美传媒沈樵 | 成年人黄国产 | 天天想你在线观看完整版电影高清 | 黑人操日本女优 |