惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
O
OpenAI News
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Webroot Blog
Webroot Blog
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
N
News | PayPal Newsroom
H
Hacker News: Front Page
博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Last Watchdog
The Last Watchdog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Heimdal Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Schneier on Security
宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
Microsoft Security Blog
Microsoft Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
GbyAI
GbyAI
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
人人都是产品经理
人人都是产品经理
P
Palo Alto Networks Blog
M
MIT News - Artificial intelligence
G
GRAHAM CLULEY
C
Check Point Blog
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
T
Troy Hunt's Blog
L
Lohrmann on Cybersecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
量子位
博客园 - 聂微东
S
Securelist
博客园 - 三生石上(FineUI控件)
F
Full Disclosure
G
Google Developers Blog
L
LINUX DO - 热门话题
P
Proofpoint News Feed
AI
AI
PCI Perspectives
PCI Perspectives

博客园 - 唐朝程序员

推荐一个winform 界面交互类库转 DBCC SHOWCONTIG、DBCC DBREINDEX。 SQL Server2005索引碎片分析和解决方法 some things 微软自带的防反编译工具dotfuscator.exe的使用 SQL查询重复数据和清除重复数据 lucene 笔记 SqlServer的汉字转拼音码的函数 windows系统事件查询 2010考研冲刺必备 2010公务员考试必备资料 url重写适用html为伪静态后真实的html无法访问的解决方法 SQL的 优化 (某篇的精简版) SQL 优化 (某篇的精简版) U影资源网原域名被封了,现在叫狐库网,启用了新的域名 vs2008 下载地址以及正式版序列号 Windows XP和Office2003通过正版验证,免去黑屏之忧 NOKIA诺基亚PC套件在2003系统上的安装方法 在.net 2.0 中使用ftp
在HttpModule中使用gzip,deflate协议对aspx页面进行压缩(转)
唐朝程序员 · 2010-04-13 · via 博客园 - 唐朝程序员

现在浏览器一般都支持gzip,deflate压缩协议 , 也就是说当服务器返回的是用gzip或deflate协议进行压缩过的内容, 浏览器将自动的进行解压缩 . 这样做可以节省大量的网络带宽,负面影响是加重了服务器的负担.

我们只是对aspx页面进行压缩 ,当然也可以压缩js和css . 但你也想用来对图片也进行压缩的话就错了 ,效果和用winzip压缩图片一样, 只能增大体积.

首先来看看一个实例 aspx页面压缩前和压缩后的页面信息
压缩前

压缩后

可以看到压缩到原来页面大小的27% 效果还是可以的.
看看具体代码


#region Usingusing System;
using
 System.Web;
using
 System.IO.Compression;#endregionnamespace BlogEngine.Core.Web.HttpModules
{
  
/// <summary>

  
/// Compresses the output using standard gzip/deflate.
  
/// </summary>

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

    void IHttpModule.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"></see> 
    
///
 that provides access to the methods, properties, and events common to 
    
///
 all application objects within an ASP.NET application.
    
/// </param>

    void IHttpModule.Init(HttpApplication context)
    {
      
if
 (BlogSettings.Instance.EnableHttpCompression)
        context.BeginRequest 
+= new
 EventHandler(context_BeginRequest);
    }
#endregion
#region Compressionprivate const string GZIP = "gzip";
    
private const string DEFLATE = "deflate"
;/// <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;
      
//压缩aspx页面

      if (app.Request.Url.OriginalString.ToUpperInvariant().Contains(".aspx"))
      {
        
//是否支持压缩协议

        if (IsEncodingAccepted(DEFLATE))
        {
          app.Response.Filter 
= new
 DeflateStream(app.Response.Filter, CompressionMode.Compress);
          SetEncoding(DEFLATE);
        }
        
else if
 (IsEncodingAccepted(GZIP))
        {
          app.Response.Filter 
= new
 GZipStream(app.Response.Filter, CompressionMode.Compress);
          SetEncoding(GZIP);
        }
      }
    }
/// <summary>

    
/// Checks the request headers to see if the specified
    
///
 encoding is accepted by the client.
    
/// </summary>

    private static bool IsEncodingAccepted(string encoding)
    {
      
return HttpContext.Current.Request.Headers["Accept-encoding"!= null && HttpContext.Current.Request.Headers["Accept-encoding"
].Contains(encoding);
    }
/// <summary>

    
/// Adds the specified encoding to the response headers.
    
/// </summary>

    
/// <param name="encoding"></param>
    private static void SetEncoding(string encoding)
    {
      HttpContext.Current.Response.AppendHeader(
"Content-encoding"
, encoding);
    }
#endregion

  }
}