激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - ASP.NET教程 - Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼

Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼

2019-09-23 10:39asp.net教程網(wǎng) ASP.NET教程

當(dāng)我們壓縮我的Response后再傳到Client端時(shí),可以明顯節(jié)省寬帶. 提升Site的性能. 現(xiàn)在的瀏覽器大部分都支持Gzip,Deflate壓縮

同時(shí)我們還可以刪除一些空白 
段,空行,注釋等以使得HTML文檔的尺寸變得更小. 讓我們先來實(shí)現(xiàn)壓縮與刪除空白類, 繼承自Stream類: 

復(fù)制代碼代碼如下:


/// <summary> 
/// CompressWhitespaceFilter 
/// </summary> 
public class CompressWhitespaceFilter : Stream 

private GZipStream _contentGZipStream; 
private DeflateStream _content_DeflateStream; 
private Stream _contentStream; 
private CompressOptions _compressOptions; 
/// <summary> 
/// Initializes a new instance of the <see cref="CompressWhitespaceFilter"/> class. 
/// </summary> 
/// <param name="contentStream">The content stream.</param> 
/// <param name="compressOptions">The compress options.</param> 
public CompressWhitespaceFilter(Stream contentStream, CompressOptions compressOptions) 

if (compressOptions == CompressOptions.GZip) 

this._contentGZipStream = new GZipStream(contentStream, CompressionMode.Compress); 
this._contentStream = this._contentGZipStream; 

else if (compressOptions == CompressOptions.Deflate) 

this._content_DeflateStream = new DeflateStream(contentStream,CompressionMode.Compress); 
this._contentStream = this._content_DeflateStream; 

else 

this._contentStream = contentStream; 

this._compressOptions = compressOptions; 

public override bool CanRead 

get { return this._contentStream.CanRead; } 

public override bool CanSeek 

get { return this._contentStream.CanSeek; } 

public override bool CanWrite 

get { return this._contentStream.CanWrite; } 

public override void Flush() 

this._contentStream.Flush(); 

public override long Length 

get { return this._contentStream.Length; } 

public override long Position 

get 

return this._contentStream.Position; 

set 

this._contentStream.Position = value; 


public override int Read(byte[] buffer, int offset, int count) 

return this._contentStream.Read(buffer, offset, count); 

public override long Seek(long offset, SeekOrigin origin) 

return this._contentStream.Seek(offset, origin); 

public override void SetLength(long value) 

this._contentStream.SetLength(value); 

public override void Write(byte[] buffer, int offset, int count) 

byte[] data = new byte[count + 1]; 
Buffer.BlockCopy(buffer, offset, data, 0, count); 
string strtext = System.Text.Encoding.UTF8.GetString(data); 
strtext = Regex.Replace(strtext, "^\s*", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline); 
strtext = Regex.Replace(strtext, "\r\n", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline); 
strtext = Regex.Replace(strtext, "<!--*.*?-->", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline); 
byte[] outdata = System.Text.Encoding.UTF8.GetBytes(strtext); 
this._contentStream.Write(outdata, 0, outdata.GetLength(0)); 


/// <summary> 
/// CompressOptions 
/// </summary> 
/// <seealso cref="http://en.wikipedia.org/wiki/Zcat#gunzip_and_zcat"/> 
/// <seealso cref="http://en.wikipedia.org/wiki/DEFLATE"/> 
public enum CompressOptions 

GZip, 
Deflate, 
None 


上面的代碼使用正則表達(dá)式替換字符串,你可以修改那些正則表達(dá)式來滿足你的需求. 我們同時(shí)使用了GZipStreamDeflateStream實(shí)現(xiàn)了壓縮. 好的,接下來與
HttpModule結(jié)合:

復(fù)制代碼代碼如下:


/// <summary> 
/// CompressWhitespaceModule 
/// </summary> 
public class CompressWhitespaceModule : IHttpModule 

#region IHttpModule Members 
/// <summary> 
/// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>. 
/// </summary> 
public void Dispose() 

// Nothing to dispose; 

/// <summary> 
/// Initializes a module and prepares it to handle requests. 
/// </summary> 
/// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param> 
public void Init(HttpApplication context) 

context.BeginRequest += new EventHandler(context_BeginRequest); 

/// <summary> 
/// Handles the BeginRequest event of the context control. 
/// </summary> 
/// <param name="sender">The source of the event.</param> 
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
void context_BeginRequest(object sender, EventArgs e) 

HttpApplication app = sender as HttpApplication; 
if (app.Request.RawUrl.Contains(".aspx")) 

HttpContext context = app.Context; 
HttpRequest request = context.Request; 
string acceptEncoding = request.Headers["Accept-Encoding"]; 
HttpResponse response = context.Response; 
if (!string.IsNullOrEmpty(acceptEncoding)) 

acceptEncoding = acceptEncoding.ToUpperInvariant(); 
if (acceptEncoding.Contains("GZIP")) 

response.Filter = new CompressWhitespaceFilter(context.Response.Filter, CompressOptions.GZip); 
response.AppendHeader("Content-encoding", "gzip"); 

else if (acceptEncoding.Contains("DEFLATE")) 

response.Filter = new CompressWhitespaceFilter(context.Response.Filter, CompressOptions.Deflate); 
response.AppendHeader("Content-encoding", "deflate"); 


response.Cache.VaryByHeaders["Accept-Encoding"] = true; 


#endregion 


HttpApplication.BeginRequest 事件是 在 ASP.NET 響應(yīng)請(qǐng)求時(shí)作為 HTTP 執(zhí)行管線鏈中的第一個(gè)事件發(fā)生。 
在WEB.CONFIG中你還需要配置: 

復(fù)制代碼代碼如下:


<httpModules> 
<add name="CompressWhitespaceModule" type="MyWeb.CompressWhitespaceModule" /> 
</httpModules> 

 

我們來看一下效果,下面沒有使用時(shí), 4.8KB

Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼

接著看,處理過后的效果,Cotent-Encoding: gzip,  filezie: 1.6KB

Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼

很簡(jiǎn)單,你可以按需求來增加更多的功能. 希望對(duì)您開發(fā)有幫助.
作者:Petter Liu

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲成年人免费网站 | 99麻豆久久久国产精品免费 | 精品国产乱码久久久久久丨区2区 | 久久夜视频 | 欧美电影在线观看 | 污片在线观看视频 | 久久久久av69精品 | 国产精品久久久免费 | 久久久久久久久久久久久久久伊免 | 久久久久久久久久久久网站 | 久久男人视频 | 欧美久久久一区二区三区 | 免费1级做55爰片l在线观看 | 精品国产一区二区三区四区在线 | 成人一级视频在线观看 | 国产二区三区四区 | 成人免费淫片 | 欧美成人性生活片 | 日本一区二区精品 | 88xx成人精品视频 | 黄色的视频免费观看 | 久久精品一区二区三区不卡牛牛 | 国产精品九九久久一区hh | 91精品国产综合久久婷婷香 | 日韩黄色一区 | 成年免费在线视频 | 精品一区二区亚洲 | 成人免费久久网 | 精品无码一区在线观看 | 色七七久久影院 | www.17c亚洲蜜桃 | 国产午夜精品一区二区三区免费 | 羞羞视频一区二区 | 亚洲小视频网站 | 国产一国产精品一级毛片 | 久久9色 | 性片久久 | 久久免费视频3 | 海角在线观看91一区二区 | 国产精品一区免费在线观看 | 精品国产一区二区三区四区在线 |