好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁(yè) > 知識(shí)庫(kù) > Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼

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

熱門標(biāo)簽:陜西人工外呼系統(tǒng)哪家好 浙江外呼系統(tǒng)怎么安裝 海外地圖標(biāo)注門市標(biāo) 地圖標(biāo)注多個(gè)行程 云南外呼電銷機(jī)器人系統(tǒng) 山西防封卡電銷卡套餐 銅川小型外呼系統(tǒng)運(yùn)營(yíng)商 上海楊浦怎么申請(qǐng)申請(qǐng)400電話 廈門商鋪地圖標(biāo)注
同時(shí)我們還可以刪除一些空白
段,空行,注釋等以使得HTML文檔的尺寸變得更小. 讓我們先來(lái)實(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á)式來(lái)滿足你的需求. 我們同時(shí)使用了GZipStream與DeflateStream實(shí)現(xiàn)了壓縮. 好的,接下來(lái)與
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>

我們來(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

您可能感興趣的文章:
  • asp.net 通過(guò)httpModule計(jì)算頁(yè)面的執(zhí)行時(shí)間
  • asp.net通過(guò)HttpModule自動(dòng)在Url地址上添加參數(shù)
  • HttpHandler HttpModule入門篇
  • 從請(qǐng)求管道深入剖析HttpModule的實(shí)現(xiàn)機(jī)制圖文介紹

標(biāo)簽:常州 孝感 朔州 信陽(yáng) 西雙版納 萊蕪 許昌 自貢

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼》,本文關(guān)鍵詞  Asp.net,使用,HttpModule,壓縮,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Asp.net使用HttpModule壓縮并刪除空白Html請(qǐng)求的實(shí)現(xiàn)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 一个人www在线观看免费下载| 蜜桃成人无码AV在线观看一电影| 动漫激情大尺度无遮掩视频| 国产电影在线网站| 一面膜胸口一面吃下部位片段| 丁香六月婷婷七月激情| 欧美熟妇脚交XXoo| 性色视频| 欧美成人中文字幕在线视频| 青楼大胸奶头晃着喷奶水H漫画| 国产欧美亚洲精品第一页久久肉| 911精品产国品一二| 婷婷狠狠五月天小说免费| 日本伦理大片中文字幕| 国产AV精品深夜福利网站| 大胸护士在线观看| 国产国拍精品亚洲AV片男人| free性欧美交| 免费看黄软件下载| 中国gayboy| 羞羞漫画网址入口| 日日射夜夜| 乱Lun第12部| 男人鸡捅女人| 好爽?要高潮了?深点朱志鑫| japanfreesex18-19护士| 夜夜夜夜猛噜噜噜噜噜| 和丝袜岳婆的婬乱生活| 伊对下载2023官方正版免费下载| 国产综合精品一区二区| 一个人的视频免费观看| 教官H高H污肉1v1| 舌头伸进去?的我的B看看| 日韩中文字幕一区二区不卡| 亚洲人AV天堂AI换脸刘亦菲| 男男Gay做受XXXⅩ106| 特级**毛片| 伸进老师内裤摸黑森林动态图| 久久久久久精品一级毛片免费粉色 | 男女激情作爱揉胸中国床上| 全彩里番acg海贼王娜美|