【Azure 应用服务】App Service站点Header头中的中文信息显示乱码?当下载文件时,文件名也是乱码?
问题描述
在本地开发的站点,响应头中的中文可以正常显示,部署到Azure App Service站点后,响应中文乱码。通过多方面验证,在代码中设置Response的Headers会显示乱码,而直接配置在Web.Config中的Header则能正常显示。
代码中写的中文会乱码
context.HttpContext.Response.Headers.Add("ChineseTest", "中");
在web.config中的正常显示
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<httpProtocol>
<customHeaders>
<add name="abc" value="中"/>
</customHeaders>
</httpProtocol>
</system.webServer>
问题解决
#使用UTF8来编码中文字符
context.HttpContext.Response.Headers.Add("ChineseTest", HttpUtility.UrlEncode("中", System.Text.Encoding.UTF8));
#如果是需要下载文件或者设置文件名称,所以需要使用Content-Disposition头
string headerValue = "attachment;";
headerValue += " filename=" + HttpUtility.UrlEncode("中.txt", System.Text.Encoding.UTF8) + ";";
headerValue += " filename*=utf-8''" + HttpUtility.UrlEncode("中.txt", System.Text.Encoding.UTF8);
HttpContext.Response.Headers.Add("Content-Disposition", headerValue);
问题分析
Http Header默认只接受ISO-8859-1的编码,对于非ISO-8859-1/ASCII编码的字符,如果直接传输的话会由于编码不一致导致显示乱码的问题。所以建议采用URL encode的方式,将中文通过这种方式传输,那么浏览器收到该header时,会解码之后显示。 如:HttpUtility.UrlEncode("中文字符", System.Text.Encoding.UTF8)
另外,由于不同的浏览器支持的编码不太相同,目前市面上的大部分浏览器都是支持使用utf-8编码方式的,所以针对Safari浏览器显示异常的问题,在Content-Disposition后续推出的filename*参数中支持指定的编码方式对filename进行处理。 操作方式正是如下代码:
string headerValue = "attachment;";
headerValue += " filename=" + HttpUtility.UrlEncode("中.txt", System.Text.Encoding.UTF8) + ";";
headerValue += " filename*=utf-8''" + HttpUtility.UrlEncode("中.txt", System.Text.Encoding.UTF8);
HttpContext.Response.Headers.Add("Content-Disposition", headerValue);
参考资料
Content-Disposition头部中文编码测试:http://imushan.com/2019/04/10/network/Content-Disposition%E5%A4%B4%E9%83%A8%E4%B8%AD%E6%96%87%E7%BC%96%E7%A0%81%E6%B5%8B%E8%AF%95/
HTTP协议header中Content-Disposition中文文件名乱码:https://my.oschina.net/pingpangkuangmo/blog/376332
【Azure 应用服务】App Service站点Header头中的中文信息显示乱码?当下载文件时,文件名也是乱码?的更多相关文章
- C#中解决Response.AddHeader("Content-Disposition", "attachment; filename=" + filename)下载文件时文件名乱码的问题
问题:下载文件时文件名乱码怎么解决? 在C#写后台代码过程中,经常遇到下载文件出现文件名乱码的问题,在网上找了很多方法,总是存在浏览器不兼容的问题,当IE浏览器不乱码时,火狐浏览器就会乱码,后来经过反 ...
- Java下载文件时文件名中的中文变成下划线,其他正常
将 utf-8 转换成 ISO8859-1 编码 response.addHeader("Content-Disposition", "attachment;filena ...
- 【应用服务 App Service】快速获取DUMP文件(App Service for Windows(.NET/.NET Core))
问题情形 当应用在Azure 应用服务App Service中运行时,有时候出现CPU,Memory很高,但是没有明显的5XX错误和异常日志,有时就是有异常但是也不能明确的指出具体的代码错误.当面临这 ...
- 转载: 正确处理浏览器在下载文件时HTTP头的编码问题(Content-Disposition)
最近在做一个下载工具时,发现CSDN上的资源下载时竟然没有被拦截到,经过分析,终于有了一个发现,解决了我之前做文件下载时的乱码问题,所以转载这篇释疑文章,希望有人可以看到,可以从中得到帮助,也用来备忘 ...
- 【应用服务 App Service】Azure App Service 中如何安装mcrypt - PHP
问题描述 Azure App Service (应用服务)如何安装PHP的扩展 mcrypt(mcrypt 是php里面重要的加密支持扩展库) 准备条件 创建App Service, Runtime ...
- 【Azure 应用服务】App Service For Windows 环境中部署Python站点后,如何继续访问静态资源文件呢(Serving Static Files)?
问题描述 当创建一个App Service 后,运行时环境和版本选择Windows 和 Python 3.6. 登录Kudu 站点查看,默认的文件有 web.config, hostingstart- ...
- 【应用服务 App Service】在Azure App Service中使用WebSocket - PHP的问题 - 如何使用和调用
问题描述 在Azure App Service中,有对.Net,Java的WebSocket支持的示例代码,但是没有成功的PHP代码. 以下的步骤则是如何基于Azure App Service实现PH ...
- 【Azure 应用服务】PHP应用部署在App Service for Linux环境中,上传文件大于1MB时,遇见了413 Request Entity Too Large 错误的解决方法
问题描述 在PHP项目部署在App Service后,上传文件如果大于1MB就会遇见 413 Request Entity Too Large 的问题. 问题解决 目前这个问题,首先需要分析应用所在的 ...
- 【应用服务 App Service】App Service中抓取网络日志
问题描述 众所周知,Azure App Service是一种PaaS服务,也就是说,IaaS层面的所有内容都由平台维护,所以使用App Service的我们根本无法触碰到远行程序的虚拟机(VM), 所 ...
随机推荐
- CSS 水平滚动条 bug & width auto increase bug
CSS 水平滚动条 bug css overflow & width auto increase bug 问题排查方式 删除可疑的模块,一步步找到问题的原因,定位问题所在 寻找可能会导致 wi ...
- TypeScript tuple 元组
TypeScript tuple 元组 元组类型允许您用固定数量的元素表示数组,这些元素的类型是已知的,但不必相同. "use strict"; /** * * @author x ...
- 如何使用 js 实现一个 throttle 函数
如何使用 js 实现一个 throttle 函数 原理 实现方式 "use strict"; /** * * @author xgqfrms * @license MIT * @c ...
- algorithm & bitwise operation & the best leetcode solutions
algorithm & bitwise operation & the best leetcode solutions leetcode 136 single-number the b ...
- JavaScript 如何使用 setTimeout 实现 setInterval
JavaScript 如何使用 setTimeout 实现 setInterval website multi content page setIntervalSimulator "use ...
- dark theme website
dark theme website css var dark theme prefers-color-scheme https://developer.mozilla.org/en-US/docs/ ...
- auto scroll bottom in js
auto scroll bottom in js autoScrollToBottom() { let box = document.querySelector(`[data-dom="ch ...
- Open API collection
Open API collection online API https://developer.github.com/v3/ https://developer.github.com/v4 http ...
- 「NGK每日快讯」11.25日NGK公链第23期官方快讯!
- 直播预告 | 全面的审计分析和权限管控——CloudQuery年终发布!
2020年9月,CloudQuery 发布. 针对开发.运维人员面临的如何高效便捷访问.操作管理数据的问题,我们设计并研发了云原生安全数据操作平台,CloudQuery 就此诞生! 2020年结束之际 ...