locked
enabling gzip compression for web service requests RRS feed

  • Question

  • User-1318870877 posted

    My project has a web reference to remote .net 1.1 web service. I used visual studio's automatic code designer to create the proxy class for the service. The remote web service is hosted using IIS 6 and gzip compression is facilitated through a F5 networks load balancer. Connections to the web service are expensive because the web service returns are a large XML document and I need to enable compression so I can reduce the download times. I've studied several articles on .net 2.0 web service and compression and decided to use a custom HTTP handler to create a derived instance of WebRequest in manner that is identical to the following article at CodeProject: http://www.codeproject.com/KB/aspnet/CompressibleHTTP20.aspx.

    I sucessfully implemented the derived class and was able to compile and run the project. The project sucessfully makes a request and receives a response but the request header does not contain "Accept-Encoding: gzip, deflate" and therefore the remote system is not returning a compressed response. I verified the header using Paros:

    POST http://xxxxx.xxxxxx.com/CLients.asmx HTTP/1.1
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.1433) Paros/3.2.13
    VsDebuggerCausalityData: uIDPoz95a0d2uYtEunB+qHmhNhYAAAAALB0iatwVukmJmqGRUap6nV0kfGmtNzVLsEt1KfoSya0ACQAA
    Content-Type: text/xml; charset=utf-8
    SOAPAction: "http://xxxxx.xxxxxx.com/webservices/SubmitOrder"
    Host: xxxx.xxxxxxx.com
    Content-Length: 868
    Expect: 100-continue
    Proxy-Connection: Keep-Alive

    While stepping through the code I noticed the EnableDecompression property on proxy class instance was set to false so I set it to true after my proxy class was instantiated but it didn't work. I also tried adding the following line to my custom WebRequest.Create method:


    httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");

    Interestingly I can add other attributes to the header like Accept-Range and Accept just not Accept-Range.

    I've been studying this one for hours and as far as I can tell my syntax and architecture are correct. I'm hoping somebody can point me in the right direction?

    Thanks.

     

    1    using System;  
    2    using System.Collections.Generic;  
    3    using System.Text;  
    4    using System.Net;  
    5    using System.Reflection;  
    6     
    7    // http://www.codeproject.com/KB/aspnet/CompressibleHTTP20.aspx  
    8    namespace BladeTest  
    9    {  
    10       public class CompressibleHttpRequestCreator : IWebRequestCreate  
    11       {  
    12           public CompressibleHttpRequestCreator()  
    13           {  
    14           }  
    15    
    16           WebRequest IWebRequestCreate.Create(Uri uri)  
    17           {  
    18               HttpWebRequest httpWebRequest =  
    19                   Activator.CreateInstance(typeof(HttpWebRequest),  
    20                   BindingFlags.CreateInstance | BindingFlags.Public |  
    21                   BindingFlags.NonPublic | BindingFlags.Instance,  
    22                   null, new object[] { uri, null }, null) as HttpWebRequest;  
    23    
    24               if (httpWebRequest == null)  
    25               {  
    26                   return null;  
    27               }  
    28                 
    29               httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;  
    30                             
    31               httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");  
    32                 
    33               return httpWebRequest;  
    34           }  
    35             
    36       }    
    37    
    38   }  
    
      

     

    Friday, September 5, 2008 10:26 AM

All replies

  • User-1318870877 posted

    I solved this problem and wanted to post the solution in case anybody else runs into it.

    For some reason Paros was not showing the "Accept-Encoding: gzip, deflate" attribute in the request header when I captured the request which made me think the attribute was missing. In actuality "Accept-Encoding: gzip" was being added to the header as it should have been. When I added a header manually to the request.header collection using httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); it caused an extra ".gzip" to be appended to the existing Accept-Encoding attribute resulting in "Accept-Encoding: gzip, deflate, gzip". I solved the problem by installing Fiddler and re-running my tests that showed the Accept-Encoding attribute was being added in duplicate. I removed the extra header and it worked fine.

    One more thing, even though I set the AutomaticDeompression property in the custom HTTP handler the EnableCompression property in my web service proxy class was set to false. I had to add to set [service reference].EnableCompression = true to actually enable compression. Can confirmed that when it was set to false compression was deactivated.  

    Here's the code:

     

    private static void Main(string[] args)
    {
        // use custom class to create xml request document to pass to web service
        BladeRequest request = new BladeRequest("john", "", "smith", "", "ca", BladeProducts.SRC1_GROUP_RESULTS);
        // create service reference
        ServiceReference1.OrdersSoapClient blgateway = new BladeTest.ServiceReference1.OrdersSoapClient();
        // enable compression
        blgateway.EnableDecompression = true;
        // call web method
        XmlNode response = blgateway.SubmitOrder(key, request.InnerXml, string.Empty);
    }

    I could have also added EnableDecompression to the method in proxy class which might have been cleaner but if I updated my web references the change would get blown away.

    I hope this helps.

    - CN

    Friday, September 5, 2008 3:50 PM
  • User930760585 posted

    Dear CN, 

    I have a similar issue but I've to use a thirdpart webserver onto a asp.net web application .

     so I'm not be able to change the web server, I made the assumption that  "EnableDecompression" is not in the webserver.  if I call a public method at the webserver, I got a serial of number instead an xml. for example the web server has a method that shows the web server date.  When I call it (  oWs.ObtenerHora() , the answer is  QlpoOTFBWSZTWfkCOlkAAAKYAEAA+lAgADEGTEEGmmJxakEcosrAn4u5IpwoSHyBHSyA  instead of <?xml version="1.0" encoding="utf-8" ?>
     <string xmlns="http://Wson.com.ar/">08/21/2013 18:53:07</string> ) 

     Cold you help me with that problem?

    Thanks in Advance

    Ignacio

     

     

    Wednesday, August 21, 2013 6:00 PM