Click here to Skip to main content
13,288,520 members (61,475 online)
Click here to Skip to main content
Add your own
alternative version


71 bookmarked
Posted 13 Jan 2006

HTTP compression in .NET Framework 2.0

, 1 May 2007
Rate this:
Please Sign up or sign in to vote.
An article on how to request and handle HTTP compression in .NET Framework 2.0.


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:


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 = 
            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 | 

        return httpWebRequest;


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.

      <remove prefix="http:"/>
      <add prefix="http:" 

            type="Pajocomo.Net.CompressibleHttpRequestCreator, Pajocomo" />


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.


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

You may also be interested in...

Comments and Discussions

QuestionCompressibleHttpRequestCreator - app.config vs programmatically (for Class Library App) Pin
Member 1095544418-Jul-14 1:33
memberMember 1095544418-Jul-14 1:33 
AnswerRe: CompressibleHttpRequestCreator - app.config vs programmatically (for Class Library App) Pin
Paulo Morgado18-Jul-14 7:19
professionalPaulo Morgado18-Jul-14 7:19 
GeneralRe: CompressibleHttpRequestCreator - app.config vs programmatically (for Class Library App) Pin
Member 1095544421-Jul-14 22:19
memberMember 1095544421-Jul-14 22:19 
GeneralRe: CompressibleHttpRequestCreator - app.config vs programmatically (for Class Library App) Pin
Paulo Morgado22-Jul-14 15:07
professionalPaulo Morgado22-Jul-14 15:07 
GeneralMy vote of 5 Pin
LastMandg423-Jan-11 4:33
memberLastMandg423-Jan-11 4:33 
GeneralYou are my hero!!!!! Pin
yoni at jefco22-Nov-10 12:13
memberyoni at jefco22-Nov-10 12:13 
GeneralRe: You are my hero!!!!! Pin
Paulo Morgado22-Nov-10 14:28
memberPaulo Morgado22-Nov-10 14:28 
GeneralRe: You are my hero!!!!! Pin
yoni at jefco23-Nov-10 5:22
memberyoni at jefco23-Nov-10 5:22 
GeneralRe: You are my hero!!!!! Pin
Paulo Morgado23-Nov-10 15:10
memberPaulo Morgado23-Nov-10 15:10 
Questionhow to use compressibles [modified] Pin
animayhem026-Aug-10 2:56
memberanimayhem026-Aug-10 2:56 
AnswerRe: how to use compressibles Pin
Paulo Morgado26-Aug-10 15:06
memberPaulo Morgado26-Aug-10 15:06 
GeneralRe: how to use compressibles Pin
animayhem026-Aug-10 22:06
memberanimayhem026-Aug-10 22:06 
GeneralRe: how to use compressibles Pin
Paulo Morgado29-Aug-10 3:39
memberPaulo Morgado29-Aug-10 3:39 
GeneralRe: how to use compressibles Pin
animayhem029-Aug-10 5:40
memberanimayhem029-Aug-10 5:40 
GeneralRe: how to use compressibles Pin
Paulo Morgado31-Aug-10 14:11
memberPaulo Morgado31-Aug-10 14:11 
GeneralRe: how to use compressibles Pin
animayhem031-Aug-10 20:01
memberanimayhem031-Aug-10 20:01 
GeneralF*ing brilliant Pin
Greg Olmstead17-Mar-10 8:22
memberGreg Olmstead17-Mar-10 8:22 
GeneralRe: F*ing brilliant Pin
Paulo Morgado17-Mar-10 14:45
memberPaulo Morgado17-Mar-10 14:45 
GeneralCompression not working Pin
dizzybinty29-May-09 8:45
memberdizzybinty29-May-09 8:45 
GeneralRe: Compression not working Pin
Paulo Morgado31-May-09 13:13
memberPaulo Morgado31-May-09 13:13 
GeneralMessage Closed Pin
15-Jan-09 12:13
memberDotNetGuts15-Jan-09 12:13 
GeneralRe: Good Links for Http Compression Pin
Paulo Morgado15-Jan-09 13:53
memberPaulo Morgado15-Jan-09 13:53 
GeneralRe: Using IIS7 Pin
Matt_Izilla21-Nov-07 18:39
memberMatt_Izilla21-Nov-07 18:39 
General.Net 2.0 supports content encoding out-of-the-box Pin
bwaide30-Apr-07 4:28
memberbwaide30-Apr-07 4:28 
GeneralRe: .Net 2.0 supports content encoding out-of-the-box Pin
Paulo Morgado1-May-07 11:49
memberPaulo Morgado1-May-07 11:49 
GeneralWarnings Pin
nzeemin6-Mar-07 20:51
membernzeemin6-Mar-07 20:51 
GeneralRe: Warnings Pin
Paulo Morgado6-Mar-07 23:54
memberPaulo Morgado6-Mar-07 23:54 
GeneralCompression engine that WORKS for me! Pin
pelam1-Mar-07 13:26
memberpelam1-Mar-07 13:26 
GeneralRe: Compression engine that WORKS for me! Pin
Paulo Morgado1-Mar-07 13:51
memberPaulo Morgado1-Mar-07 13:51 
QuestionContentEncoding Pin
captainq24-Oct-06 16:31
membercaptainq24-Oct-06 16:31 
AnswerRe: ContentEncoding Pin
Paulo Morgado24-Oct-06 23:19
memberPaulo Morgado24-Oct-06 23:19 
GeneralRe: ContentEncoding Pin
captainq5-Nov-06 3:55
membercaptainq5-Nov-06 3:55 
GeneralRe: ContentEncoding Pin
Paulo Morgado5-Nov-06 5:36
memberPaulo Morgado5-Nov-06 5:36 
GeneralRe: ContentEncoding Pin
captainq5-Nov-06 9:50
membercaptainq5-Nov-06 9:50 
GeneralSystem.Runtime.Serialization.dll Pin
Jntn14-May-06 8:53
memberJntn14-May-06 8:53 
GeneralRe: System.Runtime.Serialization.dll Pin
Paulo Morgado14-May-06 11:31
memberPaulo Morgado14-May-06 11:31 
GeneralRe: System.Runtime.Serialization.dll Pin
Jntn14-May-06 23:22
memberJntn14-May-06 23:22 
GeneralRe: System.Runtime.Serialization.dll Pin
Paulo Morgado15-May-06 0:39
memberPaulo Morgado15-May-06 0:39 
Generalweb service specific exception Pin
Petr Melichar22-Apr-06 3:03
memberPetr Melichar22-Apr-06 3:03 
GeneralRe: web service specific exception Pin
Paulo Morgado14-May-06 11:20
memberPaulo Morgado14-May-06 11:20 
AnswerRe: web service specific exception Pin
realmaestro3-Apr-07 16:48
memberrealmaestro3-Apr-07 16:48 
GeneralRe: web service specific exception Pin
Paulo Morgado3-Apr-07 22:59
memberPaulo Morgado3-Apr-07 22:59 
QuestionImport problem Pin
TommyB811-Mar-06 5:09
memberTommyB811-Mar-06 5:09 
AnswerRe: Import problem Pin
Paulo Morgado12-Mar-06 2:07
memberPaulo Morgado12-Mar-06 2:07 
Using Ethereal and Fiddler, did you check if the Accept-Encoding is being sent?

You could set breakpoints in CompressibleHttpRequestCreator.Create, CompressibleHttpWebRequest.BeforeGetResponse, CompressibleHttpWebRequest.AfterGetResponse and CompressibleHttpWebResponse.GetResponseStream to see what's going on.

To make sure the right classes are instantiated, you can, also, create the WebRequest instance like this:

IWebRequestCreate webRequestCreate = new CompressibleHttpRequestCreator();
WebRequest webRequest = webRequestCreate.Create(uri);
WebResponse webResponse = webRequest.GetResponse();

You can register a IWebRequestCreate descendant for a spceified prefix in code using WebRequest.RegisterPrefix, but you can't override the definitions for HTTP and HTTPS. And you would end up with your code tightly coupled with these classes. This is the sort of thing you want plugguble.

Paulo Morgado
Portugal - Europe's West Coast
GeneralRe: Import problem Pin
TommyB812-Mar-06 2:24
memberTommyB812-Mar-06 2:24 
GeneralRe: Import problem Pin
Paulo Morgado12-Mar-06 3:01
memberPaulo Morgado12-Mar-06 3:01 
GeneralRe: Import problem Pin
TommyB812-Mar-06 3:30
memberTommyB812-Mar-06 3:30 
GeneralRe: Import problem Pin
Paulo Morgado12-Mar-06 7:37
memberPaulo Morgado12-Mar-06 7:37 
AnswerRe: Import problem Pin
TommyB812-Mar-06 7:44
memberTommyB812-Mar-06 7:44 
QuestionHelp - How to do? Pin
RSArockiam6-Feb-06 21:44
memberRSArockiam6-Feb-06 21:44 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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