使用gzip和headers压缩从Web方法调用返回的字符串数据

时间:2012-03-28 12:40:03

标签: c# .net web-services iis webmethod

我听说可以从Web方法调用压缩返回的数据。 Web方法返回一个字符串,然后我的客户端应用程序使用该字符串生成xml文档。

以下是代码段。

[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();
}

我听说过在请求标题中添加内容,例如“Accept-Encoding:gzip”但我不知道如何让它工作。有谁知道我的问题的解决方案?

提前致谢。

0 个答案:

没有答案