如果HttpResponse使用gzip进行编码,则自定义标头将被删除

时间:2019-04-12 13:38:46

标签: gzip asp.net-apicontroller httpresponsemessage

我正在返回HttpResponseMessage:

var fileInfo = this.myAppService.CreateExcelExportFile(exportModel);

Stream stream = File.OpenRead(fileInfo.FullName);

var result = this.Request.CreateResponse(HttpStatusCode.OK);

result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentLength = stream.Length;
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.Add("filename", $"{exportModel.Name}.xls");

return result;

哪个得到了这样的答复:

HTTP/1.1 200 OK
Cache-Control: private,no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Length: 628
Content-Type: application/octet-stream
Expires: 0
Server: Microsoft-IIS/10.0
Content-Disposition: attachment
filename: dfd.xls
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcS2F5IFphbmRlclxEb2N1bWVudHNcZXZlXFNvdXJjZVxJbmZvcnNIVC5HZW5lc2lzQXBpXGV4cG9ydE9mZmxpbmVTYW1wbGVzVG9FeGNlbFw3NTlmYTU4NS0yZjIzLTQyM2QtYjdhZC1lOWNlMTZjY2RiYTU=?=
X-Powered-By: ASP.NET
X-UA-Compatible: IE=edge
Date: Fri, 12 Apr 2019 13:23:24 GMT

更改myAppService.CreateExcelExportFile()的实现后 响应突然变成了这样:

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: application/octet-stream
Expires: 0
Server: Microsoft-IIS/10.0
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcS2F5IFphbmRlclxEb2N1bWVudHNcZXZlXFNvdXJjZVxJbmZvcnNIVC5HZW5lc2lzQXBpXGV4cG9ydE9mZmxpbmVTYW1wbGVzVG9FeGNlbFw3NTlmYTU4NS0yZjIzLTQyM2QtYjdhZC1lOWNlMTZjY2RiYTU=?=
X-Powered-By: ASP.NET
X-UA-Compatible: IE=edge
Date: Fri, 12 Apr 2019 13:29:04 GMT
Content-Length: 3054

..破坏了我的客户端代码,因为现在删除了我在API控制器中明确设置的所有标头:

Content-Disposition: attachment
filename: dfd.xls

怎么可能?请注意,响应已使用gzip编码。

0 个答案:

没有答案