0

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?

1
  • When I turn compression off in IIS the StreamReader.BaseStream type is System.Web.ConnectStream, when it's on it is of type System.Web.GZipWrapperStream. Commented Jun 21, 2012 at 9:31

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.