Q:

Gzip compression with ASP.Net based REST API

I have a REST api with Gzip compression enabled, it's developed using the ASP.net WebAPI library. When I use the WebAPI test tool I can see the following headers:
Date: Wed, 20 Jun 2012 14:50:14 GMT
      Content-Encoding: gzip
      X-AspNet-Version: 4.0.30319
      X-Powered-By: ASP.NET
      Content-Length: 1028

In my client application I invoke the WS :
HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(URI);
            req.ContentType = "application/json";
            req.Method = "POST";                
            req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            // Add parameters to post
            byte[] data = System.Text.Encoding.Default.GetBytes(Parameters);
            req.ContentLength = data.Length;
            System.IO.Stream os = req.GetRequestStream();
            os.Write(data, 0, data.Length);
            os.Close();

            try
                {
                    string result = null;
                    using (HttpWebResponse resp1 = req.GetResponse()
                                                  as HttpWebResponse)
                    {
                        StreamReader reader =
                            new StreamReader(resp1.GetResponseStream());
                        result = reader.ReadToEnd();                        

                        return result;
                    }
                }
            catch (WebException ex)
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(ex.Response.GetResponseStream());
                string outputData = sr.ReadToEnd().Trim();

                throw new Exception(string.Format("Error {0} From WebService call", outputData));
            }

The problem I have is that resp1.ContentEncoding is always empty. result.length is always the same if I turn gzip compression on or off on the IIS server.  What am I doing wrong?

A:

Using System.Net.WebClient instead of System.Net.WebRequest solved this issue.

