I'm trying to compress the output of a WCF restful service, using GZipStream:
I put this in my global.asax:
protected void Application_BeginRequest(object sender, EventArgs e) { HttpResponse response = HttpContext.Current.Response; string AcceptEncoding = Request.Headers["Accept-Encoding"]; if (AcceptEncoding == null) return; if (AcceptEncoding.Contains("gzip")) { Logger.MainLogger.Info("gzip actif", AcceptEncoding, "GZipEncodePage"); response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); response.AppendHeader("Content-Encoding", "gzip"); } else if (AcceptEncoding.Contains("deflate")) { Logger.MainLogger.Info("deflate actif", AcceptEncoding, "GZipEncodePage"); response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); response.AppendHeader("Content-Encoding", "deflate"); response.AppendHeader("Vary", "Content-Encoding"); } }
The problem is that the field "Content-Length" of the response header still gets the value of the uncompressed stream.
For example, when I use fiddler to see what happens:
response header:
HTTP/1.1 200 OK
Date: Thu, 16 Feb 2012 14:25:32 GMT
Cache-Control: private
Content-Length: 472810
Content-Type: application/json
Content-Encoding: gzip
Vary: Content-Encoding
But I also get a fiddler pop up with the error:
Fiddler has detected a protocol violation in session #569.
Content-Length mismatch: Response Header indicated 472 810 bytes, but server sent 152 488 bytes.
Do you know what I've done wrong ?
Thanks