1

I have heard that it might be possible to compress the returned data from a web method call. The web method returns a string, which my client application then uses to generate an xml document.

Here is a code snippet.

[WebMethod]
public string GetRegions()
{
    // Create a new string builder for which the XmlWriter will append to.
    StringBuilder output = new StringBuilder();

    // Create XmlWriter settings
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.IndentChars = "    ";

    // Use an XmlWriter and a StringBuilder for outputting the xml.
    using (XmlWriter writer = XmlWriter.Create(output, settings))
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("MyData");
        writer.WriteStartElement("Regions");

        // Get all selectable regions
        RegionList regionList = RegionList.GetSelectable();

        // Loop through every region
        foreach(Region region in regionList.GetList())
        {
            writer.WriteStartElement("Region");
            writer.WriteElementString("RegionId", region.regionID.ToString());
            writer.WriteElementString("Code", region.code);
            writer.WriteElementString("Name", region.name);
            writer.WriteEndElement();

        }

        writer.WriteEndElement();
        writer.WriteEndDocument();

    }
    // Return the xml as a string
    return output.ToString();
}

I've heard about adding something to the request headers e.g. "Accept-Encoding : gzip" but I am not sure how to get this to work. Does anyone know of a solution to my problem?

Thanks in advance.

Christian Strempfer
  • 7,291
  • 6
  • 50
  • 75
bobbo
  • 845
  • 2
  • 14
  • 24
  • 1
    You can enable compression on the virtual directory hosting the webservice in IIS Manager, then it will work out whether the client can accept compressed (GZIP) or not. – kpcrash Mar 28 '12 at 12:45
  • Related question: http://stackoverflow.com/questions/4377106/asp-net-webservice-handling-gzip-compressed-request – M.Babcock Mar 28 '12 at 12:47
  • Has this ever been resolved @bobbo ? I use compresison, but manually. I could share that if you still need to achieve something like this? – Louis van Tonder Nov 27 '13 at 08:31

0 Answers0