国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題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關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

Asp.net使用HttpModule壓縮并刪除空白Html請求的實現代碼

來源:懂視網 責編:小采 時間:2020-11-27 22:42:44
文檔

Asp.net使用HttpModule壓縮并刪除空白Html請求的實現代碼

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

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

/// <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
}

上面的代碼使用正則表達式替換字符串,你可以修改那些正則表達式來滿足你的需求. 我們同時使用了GZipStream與DeflateStream實現了壓縮. 好的,接下來與
HttpModule結合:
代碼如下:

/// <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 響應請求時作為 HTTP 執行管線鏈中的第一個事件發生。
在WEB.CONFIG中你還需要配置:
代碼如下:

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

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

OringinalTraffice

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

GZIPCompession

很簡單,你可以按需求來增加更多的功能. 希望對您開發有幫助.
作者:Petter Liu

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Asp.net使用HttpModule壓縮并刪除空白Html請求的實現代碼

Asp.net使用HttpModule壓縮并刪除空白Html請求的實現代碼:同時我們還可以刪除一些空白 段,空行,注釋等以使得HTML文檔的尺寸變得更小. 讓我們先來實現壓縮與刪除空白類, 繼承自Stream類: 代碼如下:/// <summary> /// CompressWhitespaceFilter /// </summary> public c
推薦度:
標簽: 請求 空白 壓縮
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 亚洲欧美日韩中文无线码 | 影音先锋在线视频 | 久久99网站 | 欧美国产日韩综合 | 国产在线观看精品 | 国产欧美在线观看 | 伊人毛片 | 日韩中文欧美 | 日韩av线上| 午夜日韩久久影院 | 国语清晰对白声音刺激 | 亚洲日韩欧美一区二区在线 | 精品在线观看国产 | 高清视频一区 | 国产一区二区三区日韩欧美 | 一区二区三区视频在线观看 | 欧美第一精品 | 欧区一欧区二欧区三免费 | 欧美成人高清在线视频大全 | 久久免费精品国产72精品剧情 | 久久不射电影网 | 亚洲一区二区三区成人 | 制服丝袜中文在线 | 一级a毛片免费观看久久精品 | 亚洲第一视频 | 欧美亚洲免费 | 亚洲精品高清视频 | 亚洲欧美另类国产 | 久久91精品国产99久久yfo | 亚洲欧美综合图区官网 | 国产成人精品一区二区三区 | 国产丝袜美女一区二区三区 | 欧美日韩国产综合在线 | 日韩一区二区三区在线视频 | 国产不卡在线观看 | 国产一级高清视频免费看 | 国产69精品久久 | 国产在线精品一区二区 | 韩国精品一区二区久久 | 欧美日韩一区二区三区四区 | 欧美日韩精品一区二区三区视频在线 |