1

Is anybody know, is it possible to compress my outgoing http data in IE? I perform ajax requests to a server and want to reduce amount of traffic in order to speed up my app.

Thanks, Egor

Egor4eg
  • 2,678
  • 1
  • 22
  • 41
  • can you show what type of data you are exchanging? – Umair A. Sep 28 '10 at 09:39
  • If you aren't already, I recommend taking a look as using JSON for your AJAX data exchange. MVC has features that make working with JSON very easy. – BradB Sep 28 '10 at 10:06
  • I use JSON for my requests, but sometimes it is big (about 60 kBytes). So, I want to compress it – Egor4eg Sep 28 '10 at 10:14
  • 1
    {"TableId":386,"TableKey":"3357b9a3-9305-4414-a018-568733a826fc","HeaderValues":["Operator","C:Hard Part Selection","Operator","C:Bore","Operator","C:Double Rod","Operator","C:Mounting","Operator","C:Combination Mtg","Operator","C:Rod Diameter","Operator","F:2H_BODY_LGTH","Value","Operation","Operation_Type","Formula"],"Parameters":{"PlatformId":"41","Name":"test table","StartDate":"","EndDate":""},"Type":2,"Rows":[["=","CYL","=","1.5","=","K","=","C","=","_;TC","=","62","=","62","68.7","*","$",""],["=","CYL","=","1.5","=","K","=","C","=","_;TC","=","100","=","100","56.75","*","$",""]]} – Egor4eg Sep 28 '10 at 10:16

1 Answers1

2

The following is a common way to create a compression filter attribute:

public class CompressFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext actionContext)
    {
        HttpRequestBase request = actionContext.HttpContext.Request;

        string acceptEncoding = request.Headers["Accept-Encoding"];

        if (!string.IsNullOrWhiteSpace(acceptEncoding))
        {
            acceptEncoding = acceptEncoding.ToLowerInvariant();
            HttpResponseBase response = actionContext.HttpContext.Response;

            if (acceptEncoding.Contains("gzip"))
            {
                response.AddHeader("Content-encoding", "gzip");
                response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
            }
            else if (acceptEncoding.Contains("deflate"))
            {
                response.AddHeader("Content-encoding", "deflate");
                response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
            }
        }
    }
}

Now you can decorate your controller with a [CompressFilter] attribute. It will add a compression header to the response for browsers that support it, which IIS will pick up. If you have switched on Dynamic Compression, IIS will then output compressed responses.

Carvellis
  • 3,992
  • 2
  • 34
  • 66
  • That code allows to compress server response. But I want to compress request. – Egor4eg Sep 28 '10 at 10:10
  • AFAIK, no browser is capable of implicitly compressing request calls. I suppose what you could do is find a javascript compression library and post the client-compressed data to the server and manually decompress it there. Probably the overhead that this would cost on the client is more than the time you save uploading the data. – Carvellis Sep 28 '10 at 10:51
  • yes, probably it would cost more time. But I have to check it. Do you know any javascript libraries which allow to do that? – Egor4eg Sep 28 '10 at 12:56
  • You can look at [this post](http://stackoverflow.com/questions/294297/javascript-implementation-of-gzip). However keep in mind that the result will have to be serialized (in base64 for example) which will again lose a big chunk of the compression. – Carvellis Sep 28 '10 at 13:21
  • Thanks a lot. I'm going to do these tests in several days. I'll let you know the results. – Egor4eg Sep 28 '10 at 17:27