Click here to Skip to main content
Click here to Skip to main content

HTTP compression in .NET Framework 2.0

By , 1 May 2007
 

Introduction

This article describes the implementation of a utility class that will allow HTTP requests informing the server (or any appliance in the network between the client and the server) what types of compression it can handle and uncompress the response from the server (if any) without changing the client application.

HTTP compression is very useful when the cost of the connection is high.

The factory pattern of creating WebRequest instances

The WebRequest supplies two methods to create instances:

The WebRequest class uses instances of classes that implement the IWebRequestCreate interface and are registered in the webRequestModules section in the configuration files. The Create method (called by both CreateDefault and Create) returns an initialized instance of a WebRequest descendent class capable of performing a standard request/response transaction for the protocol without needing any protocol-specific fields modified.

On the other hand, the previously created WebRequest derived instances will return WebResponse derived instances that will handle the HTTP response.

Because of the way the factory pattern is implemented, we can change the behavior of already built applications (our applications or even the .NET Framework) to request and handle HTTP compression, changing only the configuration files.

Since in versions 2.0 and above of the .NET framework already support compression, there is no need to supply WebRequestand WebResponse derived class implementations. All that's needed is a class that implements the IWebRequestCreate interface to create the WebRequestderived instance and set it up.

The code

As shown before, to add HTTP compression to our applications, we just have to build three classes:

CompressibleHttpRequestCreator

In order for the applications that use HttpWebRequest and HttpWebResponse to work without any changes, CompressibleHttpRequestCreator.Create has to return a HttpWebRequest instance. Unfortunately, there is no public construtor for HttpWebRequest or publicly accessible implementation of IWebRequestCreate.Create that creates a HttpWebRequest instance, so some reflection will be needed.

The implementation of IWebRequestCreate.Create just creates an instance of HttpWebRequest and sets its HttpWebRequest.AutomaticDecompression to accept all types of compression.

public class CompressibleHttpRequestCreator : IWebRequestCreate
{
    public CompressibleHttpRequestCreator()
    {
    }

    WebRequest IWebRequestCreate.Create(Uri uri)
    {
        HttpWebRequest httpWebRequest = 
            Activator.CreateInstance(typeof(HttpWebRequest),
            BindingFlags.CreateInstance | BindingFlags.Public | 
            BindingFlags.NonPublic | BindingFlags.Instance,
            null, new object[] { uri, null }, null) as HttpWebRequest;

        if (httpWebRequest == null)
        {
            return null;
        }

        httpWebRequest.AutomaticDecompression =DecompressionMethods.GZip | 
            DecompressionMethods.Deflate;

        return httpWebRequest;
    }
}  

Configuration

Now, to add HTTP compression support to any application, all that's needed is to add the corresponding entries to the webRequestModules section in the configuration file.

<configuration>
  <system.net>
    <webRequestModules>
      <remove prefix="http:"/>
      <add prefix="http:" 
            type="Pajocomo.Net.CompressibleHttpRequestCreator, Pajocomo" />
    </webRequestModules>
  </system.net>
</configuration>

History

When I first ported this from .NET 1.1, I completely missed the fact that HttpWebRequest and HttpWebResponse already implemented compression.

Thanks to Björn to point that out to me.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Paulo Morgado
Software Developer (Senior) Paulo Morgado
Portugal Portugal

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberLastMandg423-Jan-11 3:33 
GeneralYou are my hero!!!!!memberyoni at jefco22-Nov-10 11:13 
GeneralRe: You are my hero!!!!!memberPaulo Morgado22-Nov-10 13:28 
GeneralRe: You are my hero!!!!!memberyoni at jefco23-Nov-10 4:22 
Paulo Morgado wrote:
I believe decompression is allowed on .NET 4.0, but I'm not sure at this moment.

 
It is, but for those of us who are no longer using asmx services, and are not on 4.0 yet, its a case of "jam yesterday and jam tomorrow, but never jam today".
GeneralRe: You are my hero!!!!!memberPaulo Morgado23-Nov-10 14:10 
Questionhow to use compressibles [modified]memberanimayhem026-Aug-10 1:56 
AnswerRe: how to use compressiblesmemberPaulo Morgado26-Aug-10 14:06 
GeneralRe: how to use compressiblesmemberanimayhem026-Aug-10 21:06 
GeneralRe: how to use compressiblesmemberPaulo Morgado29-Aug-10 2:39 
GeneralRe: how to use compressiblesmemberanimayhem029-Aug-10 4:40 
GeneralRe: how to use compressiblesmemberPaulo Morgado31-Aug-10 13:11 
GeneralRe: how to use compressiblesmemberanimayhem031-Aug-10 19:01 
GeneralF*ing brilliantmemberGreg Olmstead17-Mar-10 7:22 
GeneralRe: F*ing brilliantmemberPaulo Morgado17-Mar-10 13:45 
GeneralCompression not workingmemberdizzybinty29-May-09 7:45 
GeneralRe: Compression not workingmemberPaulo Morgado31-May-09 12:13 
GeneralGood Links for Http CompressionmemberDotNetGuts15-Jan-09 11:13 
GeneralRe: Good Links for Http CompressionmemberPaulo Morgado15-Jan-09 12:53 
GeneralRe: Using IIS7memberMatt_Izilla21-Nov-07 17:39 
General.Net 2.0 supports content encoding out-of-the-boxmemberbwaide30-Apr-07 3:28 
GeneralRe: .Net 2.0 supports content encoding out-of-the-boxmemberPaulo Morgado1-May-07 10:49 
GeneralWarningsmembernzeemin6-Mar-07 19:51 
GeneralRe: WarningsmemberPaulo Morgado6-Mar-07 22:54 
GeneralCompression engine that WORKS for me!memberpelam1-Mar-07 12:26 
GeneralRe: Compression engine that WORKS for me!memberPaulo Morgado1-Mar-07 12:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130619.1 | Last Updated 1 May 2007
Article Copyright 2006 by Paulo Morgado
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid