最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

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

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:42:44
文檔

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

Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼:同時(shí)我們還可以刪除一些空白 段,空行,注釋等以使得HTML文檔的尺寸變得更小. 讓我們先來(lái)實(shí)現(xiàn)壓縮與刪除空白類(lèi), 繼承自Stream類(lèi): 代碼如下:/// <summary> /// CompressWhitespaceFilter /// </summary> public c
推薦度:
導(dǎo)讀Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼:同時(shí)我們還可以刪除一些空白 段,空行,注釋等以使得HTML文檔的尺寸變得更小. 讓我們先來(lái)實(shí)現(xiàn)壓縮與刪除空白類(lèi), 繼承自Stream類(lèi): 代碼如下:/// <summary> /// CompressWhitespaceFilter /// </summary> public c

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

/// <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á)式來(lái)滿足你的需求. 我們同時(shí)使用了GZipStream與DeflateStream實(shí)現(xiàn)了壓縮. 好的,接下來(lái)與
HttpModule結(jié)合:
代碼如下:

/// <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中你還需要配置:
代碼如下:

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

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

OringinalTraffice

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

GZIPCompession

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

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

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

Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼:同時(shí)我們還可以刪除一些空白 段,空行,注釋等以使得HTML文檔的尺寸變得更小. 讓我們先來(lái)實(shí)現(xiàn)壓縮與刪除空白類(lèi), 繼承自Stream類(lèi): 代碼如下:/// <summary> /// CompressWhitespaceFilter /// </summary> public c
推薦度:
標(biāo)簽: 請(qǐng)求 空白 壓縮
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專(zhuān)題
Top