.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Httpcompression - A generic way of checking the compression type

Posted By:nishithraj       Posted Date: November 29, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article we will see how to use HTTP Compression in asp.net using C#. Using Compression technique the page size will be very less and your website will very fast.
 

HttpCompression is the most important thing to improve the ASP.NET page performance much better. It will reduce the page size. It saves huge number of data transfer for each request.

A Httprequest looks like as follows in the request.

GET /Default.html HTTP/1.1
Host: www.http-compression.com
Accept-Encoding: gzip
User-Agent: Firefox/1.0

Here the  Accept-Encoding shows which type of compression is used.

We have two types of compressions 1) GZIP 2) Deflate.

Here the server understands the data in GZIP format. And it sends the response with the compressed data.

HTTP/1.1 200 OK
Server: IIS
Content-Type: text/html
Content-Encoding: gzip
Content-Length: 26395

[GZIP COMPRESSED DATA]

The compression methods are implemented in IIS 4.0 version. System.IO.Compression is the name space needs to implement the compression. It can be implemented in the Global.asax file to make it available globally  . It has to define at the event PreRequestHandlerExecute. dynamic contents and output caching need to be considered while the implementation time. Also we need to bypass AJAX pages. However the maximum compression rate that you can get is 90%. A

A generic way of implementing the compression. It dynamically checks the compression type.

HttpApplication app = (HttpApplication)sender;
    if (app.Context.CurrentHandler is System.Web.UI.Page && app.Request["HTTP_X_MICROSOFTAJAX"] == null)
    {
      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);
      }
    }
private static bool IsEncodingAccepted(string encoding)
  {
    HttpContext context = HttpContext.Current;
    return context.Request.Headers["Accept-encoding"] != null && context.Request.Headers["Accept-encoding"].Contains(encoding);
  }
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend