ERR_CONTENT_DECODING_FAILED在尝试使用HttpResponseMessage返回gzip时发生错误。

huangapple go评论64阅读模式
英文:

ERR_CONTENT_DECODING_FAILED when trying to return gzip with HttpResponseMessage

问题

在浏览器运行时出现'ERR_CONTENT_DECODING_FAILED'错误:

HttpResponseMessage response = Request.CreateResponse();
response.Content = new StringContent("H4sIAAAAAAAAAytJLS5RKIETAAP1QQcPAAAA");
response.StatusCode = HttpStatusCode.OK;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
response.Content.Headers.ContentType.CharSet = Encoding.UTF8.HeaderName;
response.Headers.Add("Vary", "Accept-Encoding");
response.Content.Headers.ContentEncoding.Clear();
response.Content.Headers.ContentEncoding.Add("gzip");
return response;

H4sIAAAAAAAAAytJLS5RKIETAAP1QQcPAAAA 是 'test test test';

期望在Chrome中的响应是 'test test test'。

英文:

Getting 'ERR_CONTENT_DECODING_FAILED' in the browser when running:

                HttpResponseMessage response = Request.CreateResponse();
                response.Content = new StringContent("H4sIAAAAAAAAAytJLS5RKIETAAP1QQcPAAAA");
                response.StatusCode = HttpStatusCode.OK;
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                response.Content.Headers.ContentType.CharSet = Encoding.UTF8.HeaderName;
                response.Headers.Add("Vary", "Accept-Encoding");
                response.Content.Headers.ContentEncoding.Clear();
                response.Content.Headers.ContentEncoding.Add("gzip");
                return response;

H4sIAAAAAAAAAytJLS5RKIETAAP1QQcPAAAA is 'test test test'

Expecting the response to be 'test test test' in Chrome

答案1

得分: 1

你有一个错误的假设。字符串 "H4sIAAAAAAAAAytJLS5RKIETAAP1QQcPAAAA" 本身不是gzip数据。即使你调整HTTP标头,这个字符串不会变成gzip数据。你只是告诉网络浏览器你发送给它的内容是某种(gzip)实际上并不是的东西。

想一想。gzip流数据只是一些二进制数据流。它不是以任何形式的文本存在,只是一些二进制数据。(类似地,你的程序的 .exe 或 .dll 文件也是 - 大部分情况下 - 不是以任何形式的文本存在的二进制数据。)所以,一个字符串实际上不能直接保存gzip数据。

那么,这个 "H4sIAAAAAAAAAytJLS5RKIETAAP1QQcPAAAA" 字符串是什么呢?它是二进制数据的 Base64编码。将这个字符串进行Base64解码后,得到以下字节(以十六进制表示):

1F 8B 08 00 00 00 00 00  00 03 2B 49 2D 2E 51 28
81 13 00 03 F5 41 07 0F  00 00 00

这些字节就是你的实际gzip数据。

所以,解决你的问题的一个可能方案是首先将你得到的Base64字符串解码为一个字节数组。然后,如果你真的想将字节数组中的gzip数据原封不动地发送给网络浏览器,你应该使用 ByteArrayContent 而不是 StringContent(因为现在你有了一个字节数组而不是字符串)。不要忘记适当地设置HTTP标头。

英文:

You made a false assumption. The string "H4sIAAAAAAAAAytJLS5RKIETAAP1QQcPAAAA" itself is not gzip data. Even if you twiddle the HTTP headers just right, the fact that this string is not gzip data doesn't change. You just told the web browser the content you sent to it is something (gzip) that it really is not.

Think about it. Gzip stream data is just some binary data stream. It's not text in any shape or form, it's just some binary data. (In a similar way, the .exe or .dll file of your program is also -mostly- binary data that is not text in any shape or form, either.) So, a string cannot really hold the actual gzip data directly.

So, what is this "H4sIAAAAAAAAAytJLS5RKIETAAP1QQcPAAAA" string then? It is a Base64 encoding of binary data. Base64-decoding this string yields the bytes (in hex notation):

1F 8B 08 00 00 00 00 00  00 03 2B 49 2D 2E 51 28
81 13 00 03 F5 41 07 0F  00 00 00

These bytes are your actual gzip data.

So, one possible solution to your problem is to first decode the Base64 string you got into a byte array. And then, if you really want to send the gzip data in the byte array as-is to the web browser, you would use a ByteArrayContent instead of a StringContent (since you have now a byte array instead of a string). Don't forget to set the http headers appropriately.

答案2

得分: 0

你不应该添加 ContentEncoding.Add("gzip"); 并发送压缩的文本。最好的方法是配置你的 web 服务器来压缩内容,它会知道何时需要压缩,何时不需要,因为并非所有内容都应该被压缩... 例如,已经压缩过的图像将原样发送。对于你的情况,你的 web 服务器可能会重新压缩已经压缩过的文本...

要调试你的问题,请安装一个 Chrome 扩展 ModHeader - 修改 HTTP 头部 来禁用 GZIP 编码。

英文:

You should not add ContentEncoding.Add("gzip"); and send compressed text like this. The best approach is to configure your web server to compress the content and it will know when is needed to compress it or not, because not everything will be sent compress... for example already compressed images will be sent as they are. Is possible that your webserver to re-compress your already compressed text for your case example...

To debug your problem install a Chrome extension ModHeader - Modify HTTP headers to disable the GZIP encoding.

huangapple
  • 本文由 发表于 2023年6月11日 21:00:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定