HTTP Compression problems on IIS7
Posted
by
Jonathan Wood
on Stack Overflow
See other posts from Stack Overflow
or by Jonathan Wood
Published on 2010-12-21T05:08:37Z
Indexed on
2010/12/21
12:54 UTC
Read the original article
Hit count: 250
I've spent quite a bit of time on this but seem to be going nowhere. I have a large page that I really want to speed up. The obvious place to start seems to be HTTP compression, but I just can't seem to get it to work for me.
After considerable searching, I've tried several variations of the code below. It kind of works, but after refreshing the browser, the results seem to fall apart. They were turning to garbage when the page used caching. If I turn off caching, then the page seems right but I lose my CSS formatting (stored in a separate file) and get an error that an included JS file contains invalid characters.
Most of the resources I've found on the Web were either very old or focused on accessing IIS directly. My page is running on a shared hosting account and I do not have direct access to IIS7, which it's running on.
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Implement HTTP compression
if (Request["HTTP_X_MICROSOFTAJAX"] == null) // Avoid compressing AJAX calls
{
// Retrieve accepted encodings
string encodings = Request.Headers.Get("Accept-Encoding");
if (encodings != null)
{
// Verify support for or gzip (deflate takes preference)
encodings = encodings.ToLower();
if (encodings.Contains("gzip") || encodings == "*")
{
Response.Filter = new GZipStream(Response.Filter,
CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "gzip");
Response.Cache.VaryByHeaders["Accept-encoding"] = true;
}
else if (encodings.Contains("deflate"))
{
Response.Filter = new DeflateStream(Response.Filter,
CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "deflate");
Response.Cache.VaryByHeaders["Accept-encoding"] = true;
}
}
}
}
Is anyone having better success with this?
© Stack Overflow or respective owner