I want to zipping response in my Asp.Net
web site. and I wrote this code:
public static void CompressPage(HttpRequest Request, HttpResponse Response)
{
string acceptEncoding = Request.Headers["Accept-Encoding"];
Stream prevUncompressedStream = Response.Filter;
if (acceptEncoding.IsEmpty())
{
return;
}
acceptEncoding = acceptEncoding.ToLower();
if (acceptEncoding.Contains("gzip"))
{
Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "gzip");
}
else if (acceptEncoding.Contains("deflate"))
{
Response.Filter = new DeflateStream(prevUncompressedStream, CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "deflate");
}
}
and call it it Page_Load
event:
protected void Page_Load(object sender, EventArgs e)
{
...
ZipHtmlPage.CompressPage(Request, Response);
}
The problem is when I run the code with and without above code in Page_Load
the size of response does not change.
Where is the problem?
Thanks
Edit 1)
I think that "Content-Encoding", "gzip"
doesn't add to header:
I don't know why?
Edit 2)
When I use HttpModule
for doing http compression:
public class CompressModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpContext context = HttpContext.Current;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
}
public void Dispose()
{
}
}
I got this in every pages: