服务器响应中缺少 ContentType HTTP 标头

发布于 2025-01-07 04:30:57 字数 835 浏览 1 评论 0原文

我有一个 ASP.NET MVC Web 操作,它返回一个简单的 zip 文件。 Responce.ContentType 属性手动设置为“text/xml; charset=utf-8; gzip”。该标头值是在将响应内容写入输出流之前设置的。 Web 项目托管在 Windows Azure 托管上。问题是有时服务器返回的响应缺少 ContentType 标头字段,这会导致客户端出现问题。不知道这可能是什么原因。当我在本地运行相同的 Web 项目时 - 一切正常,ContentType 字段具有正确的值。示例网络操作代码:

public void GetData()
{
            Response.ContentType = "text/xml; charset=utf-8; gzip";
            XDocument xml = new XDocument(...);//some large XML file
            byte[] byteData = Encoding.UTF8.GetBytes(xml.ToString());
            Stream outputStream = Response.OutputStream;
            GZipStream compressedzipStream = new GZipStream(outputStream, CompressionMode.Compress);
            compressedzipStream.Write(byteData, 0, byteData.Length);
            compressedzipStream.Close();
}

任何帮助将不胜感激。

I have a ASP.NET MVC web action which returns a simple zip file. The Responce.ContentType property is manually set to "text/xml; charset=utf-8; gzip". This header value is set before writing response content to the output stream. Web project is hosted on Windows Azure hosting. The problem is that sometimes server returns response with missing ContentType header field, this causes issues on the client side. Having no idea what could be the reason of it. When I run same web project locally - everything works fine, ContentType field has proper value. Sample web action code:

public void GetData()
{
            Response.ContentType = "text/xml; charset=utf-8; gzip";
            XDocument xml = new XDocument(...);//some large XML file
            byte[] byteData = Encoding.UTF8.GetBytes(xml.ToString());
            Stream outputStream = Response.OutputStream;
            GZipStream compressedzipStream = new GZipStream(outputStream, CompressionMode.Compress);
            compressedzipStream.Write(byteData, 0, byteData.Length);
            compressedzipStream.Close();
}

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤寂小茶 2025-01-14 04:30:57

您可以编写自定义操作结果:

public class CompressedXDocumentResult : FileResult
{
    private readonly XDocument _xdoc;
    public CompressedXDocumentResult(XDocument xdoc)
        : base("text/xml; charset=utf-8; gzip")
    {
        _xdoc = xdoc;
    }

    protected override void WriteFile(HttpResponseBase response)
    {
        using (var gzip = new GZipStream(response.OutputStream, CompressionMode.Compress))
        {
            var buffer = Encoding.UTF8.GetBytes(_xdoc.ToString());
            gzip.Write(buffer, 0, buffer.Length);
        }
    }
}

然后:

public ActionResult GetData()
{
    XDocument xml = ...
    return new CompressedXDocumentResult(xml);
}

另请注意 text/xml;字符集=utf-8; gzip 不是标准的 HTTP Content-Type 标头。因此,除非您编写一些能够理解它的自定义客户端,否则任何标准浏览器都不太可能能够解析它。

如果您想表明响应已压缩,您最好使用 内容编码 标头。您可以直接在 IIS 级别激活动态内容压缩,而不是打扰您的代码,或者如果您无法访问 IIS,您可以简单地编写一个自定义操作过滤器

[OutputCompress]
public ActionResult GetData()
{
    XDocument xml = ...
    byte[] buffer = Encoding.UTF8.GetBytes(xml.ToString());
    return File(buffer, "text/xml; charset=utf-8");
}

You could write a custom action result:

public class CompressedXDocumentResult : FileResult
{
    private readonly XDocument _xdoc;
    public CompressedXDocumentResult(XDocument xdoc)
        : base("text/xml; charset=utf-8; gzip")
    {
        _xdoc = xdoc;
    }

    protected override void WriteFile(HttpResponseBase response)
    {
        using (var gzip = new GZipStream(response.OutputStream, CompressionMode.Compress))
        {
            var buffer = Encoding.UTF8.GetBytes(_xdoc.ToString());
            gzip.Write(buffer, 0, buffer.Length);
        }
    }
}

and then:

public ActionResult GetData()
{
    XDocument xml = ...
    return new CompressedXDocumentResult(xml);
}

Also note that text/xml; charset=utf-8; gzip is not a standard HTTP Content-Type header. So unless you write some custom client that will understand it, it is unlikely that any standard browser will be able to parse it.

If you wanted to indicate that the response is compressed you'd better use the Content-Encoding header. You could either activate compression for dynamic contents directly at IIS level and don't bother in your code or if you don't have access to IIS you could simply write a custom action filter:

[OutputCompress]
public ActionResult GetData()
{
    XDocument xml = ...
    byte[] buffer = Encoding.UTF8.GetBytes(xml.ToString());
    return File(buffer, "text/xml; charset=utf-8");
}
眼眸里的快感 2025-01-14 04:30:57

试试这个:

Response.Clear();    
Response.ContentType = "text/xml; charset=utf-8; gzip";

try this:

Response.Clear();    
Response.ContentType = "text/xml; charset=utf-8; gzip";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文