Click here to Skip to main content
6,595,444 members and growing! (21,366 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

HTTP compression in .NET Framework 2.0

By Paulo Morgado

An article on how to request and handle HTTP compression in .NET Framework 2.0.
C#, XML, Windows, .NET 2.0, ASP.NET, WebForms, VS2005, Dev
Posted:13 Jan 2006
Updated:1 May 2007
Views:89,910
Bookmarked:61 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 5.20 Rating: 4.22 out of 5
2 votes, 11.8%
1

2

3
4 votes, 23.5%
4
11 votes, 64.7%
5

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


Member

Paulo has a bachelor degree in Electronics and Telecommunications (Digital Systems) by the Instituto Superior de Engenharia de Lisboa and a degree of licentiate in Informatics by the Faculdade de Ciências da Universidade Nova de Lisboa and na MCSD for Microsoft .NET certification.


Paulo has also a MCSD for the .NET Framework certification.


Paulo works at Espírito Santo Informática, ACE as a developer/software architect.


Paulo contributes in the Microsoft Portuguese newsgroups and the PontoNetPT community answering questions about software development posted in the (with special focus on the .NET framework).


His growing interest in software architecture drives his participation in the Portuguese Group of Software Architecture – GASP.


His contributions to the community has earned him Microsoft's Most Valuable Professional award.


Blog: http://www.PauloMorgado.NET/


Occupation: Software Developer (Senior)
Company: Paulo Morgado
Location: Portugal Portugal

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 42 (Total in Forum: 42) (Refresh)FirstPrevNext
GeneralCompression not working Pinmemberdizzybinty8:45 29 May '09  
GeneralRe: Compression not working PinmemberPaulo Morgado13:13 31 May '09  
GeneralGood Links for Http Compression PinmemberDotNetGuts12:13 15 Jan '09  
GeneralRe: Good Links for Http Compression PinmemberPaulo Morgado13:53 15 Jan '09  
GeneralRe: Using IIS7 PinmemberMatt_Izilla18:39 21 Nov '07  
General.Net 2.0 supports content encoding out-of-the-box Pinmemberbwaide4:28 30 Apr '07  
GeneralRe: .Net 2.0 supports content encoding out-of-the-box PinmemberPaulo Morgado11:49 1 May '07  
GeneralWarnings Pinmembernzeemin20:51 6 Mar '07  
GeneralRe: Warnings PinmemberPaulo Morgado23:54 6 Mar '07  
GeneralCompression engine that WORKS for me! Pinmemberpelam13:26 1 Mar '07  
GeneralRe: Compression engine that WORKS for me! PinmemberPaulo Morgado13:51 1 Mar '07  
QuestionContentEncoding Pinmembercaptainq16:31 24 Oct '06  
AnswerRe: ContentEncoding PinmemberPaulo Morgado23:19 24 Oct '06  
GeneralRe: ContentEncoding Pinmembercaptainq3:55 5 Nov '06  
GeneralRe: ContentEncoding PinmemberPaulo Morgado5:36 5 Nov '06  
GeneralRe: ContentEncoding Pinmembercaptainq9:50 5 Nov '06  
GeneralSystem.Runtime.Serialization.dll PinmemberJntn8:53 14 May '06  
GeneralRe: System.Runtime.Serialization.dll PinmemberPaulo Morgado11:31 14 May '06  
GeneralRe: System.Runtime.Serialization.dll PinmemberJntn23:22 14 May '06  
GeneralRe: System.Runtime.Serialization.dll PinmemberPaulo Morgado0:39 15 May '06  
Generalweb service specific exception PinmemberPetr Melichar3:03 22 Apr '06  
GeneralRe: web service specific exception PinmemberPaulo Morgado11:20 14 May '06  
AnswerRe: web service specific exception Pinmemberrealmaestro16:48 3 Apr '07  
GeneralRe: web service specific exception PinmemberPaulo Morgado22:59 3 Apr '07  
QuestionImport problem PinmemberTommyB85:09 11 Mar '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 May 2007
Editor: Sean Ewington
Copyright 2006 by Paulo Morgado
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project