问题描述

在本地开发的站点,响应头中的中文可以正常显示,部署到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头中的中文信息显示乱码?当下载文件时,文件名也是乱码?的更多相关文章

  1. C#中解决Response.AddHeader("Content-Disposition", "attachment; filename=" + filename)下载文件时文件名乱码的问题

    问题:下载文件时文件名乱码怎么解决? 在C#写后台代码过程中,经常遇到下载文件出现文件名乱码的问题,在网上找了很多方法,总是存在浏览器不兼容的问题,当IE浏览器不乱码时,火狐浏览器就会乱码,后来经过反 ...

  2. Java下载文件时文件名中的中文变成下划线,其他正常

    将 utf-8 转换成 ISO8859-1 编码 response.addHeader("Content-Disposition", "attachment;filena ...

  3. 【应用服务 App Service】快速获取DUMP文件(App Service for Windows(.NET/.NET Core))

    问题情形 当应用在Azure 应用服务App Service中运行时,有时候出现CPU,Memory很高,但是没有明显的5XX错误和异常日志,有时就是有异常但是也不能明确的指出具体的代码错误.当面临这 ...

  4. 转载: 正确处理浏览器在下载文件时HTTP头的编码问题(Content-Disposition)

    最近在做一个下载工具时,发现CSDN上的资源下载时竟然没有被拦截到,经过分析,终于有了一个发现,解决了我之前做文件下载时的乱码问题,所以转载这篇释疑文章,希望有人可以看到,可以从中得到帮助,也用来备忘 ...

  5. 【应用服务 App Service】Azure App Service 中如何安装mcrypt - PHP

    问题描述 Azure App Service (应用服务)如何安装PHP的扩展 mcrypt(mcrypt 是php里面重要的加密支持扩展库) 准备条件 创建App Service, Runtime ...

  6. 【Azure 应用服务】App Service For Windows 环境中部署Python站点后,如何继续访问静态资源文件呢(Serving Static Files)?

    问题描述 当创建一个App Service 后,运行时环境和版本选择Windows 和 Python 3.6. 登录Kudu 站点查看,默认的文件有 web.config, hostingstart- ...

  7. 【应用服务 App Service】在Azure App Service中使用WebSocket - PHP的问题 - 如何使用和调用

    问题描述 在Azure App Service中,有对.Net,Java的WebSocket支持的示例代码,但是没有成功的PHP代码. 以下的步骤则是如何基于Azure App Service实现PH ...

  8. 【Azure 应用服务】PHP应用部署在App Service for Linux环境中,上传文件大于1MB时,遇见了413 Request Entity Too Large 错误的解决方法

    问题描述 在PHP项目部署在App Service后,上传文件如果大于1MB就会遇见 413 Request Entity Too Large 的问题. 问题解决 目前这个问题,首先需要分析应用所在的 ...

  9. 【应用服务 App Service】App Service中抓取网络日志

    问题描述 众所周知,Azure App Service是一种PaaS服务,也就是说,IaaS层面的所有内容都由平台维护,所以使用App Service的我们根本无法触碰到远行程序的虚拟机(VM), 所 ...

随机推荐

  1. JVM 报 GC Overhead limit exceeded 是什么意思?

    默认情况下,并不是等堆内存耗尽,才会报 OutOfMemoryError,而是如果 JVM 觉得 GC 效率不高,也会报这个错误. 那么怎么评价 GC 效率不高呢?来看下源码: 呢?来看下源码gcOv ...

  2. Google Chrome 怎么在退出时自动删除历史记录

    1 Google Chrome 怎么在退出时自动删除历史记录 https://chrome.google.com/webstore/detail/clickclean/ghgabhipcejejjmh ...

  3. web online code editor All In One

    web online code editor All In One 在线代码编辑器 Monaco Editor 摩纳哥编辑器 ️ 22.1k The Monaco Editor is the code ...

  4. Beacon API

    Beacon API User Tracking https://caniuse.com/#feat=beacon Question & Solution Beacon API 不会延缓网页卸 ...

  5. queueMicrotask & EventLoop & macrotask & microtask

    queueMicrotask https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/queueMicro ...

  6. Node.js & module.exports & exports

    Node.js & module.exports & exports https://www.cnblogs.com/xgqfrms/p/9493550.html exports &a ...

  7. js in depth & prototype & __proto__

    js in depth & prototype & proto 实例的 proto 与 父类的 prototype,同时指向 父类的构造函数: https://hackernoon.c ...

  8. int和Integer的比较详解

    说明: int为基本类型,Integer为包装类型: 装箱: 基本类型---> 包装类型 int ---> Integer 底层源码: .intValue() 拆箱: 包装类型---> ...

  9. 开启算法之路,还原题目,用debug调试搞懂每一道题

    文章简述 大家好,本篇是个人的第 3 篇文章. 承接第一篇文章<手写单链表基础之增,删,查!附赠一道链表题>,在第一篇文章中提过,在刷算法题之前先将基础知识过一遍,这样对后面的做算法题是很 ...

  10. @media屏幕适应

    /** 屏幕特殊处理 我们用min-width时,小的放上面大的在下面,同理如果是用max-width那么就是大的在上面,小的在下面 **/ @media screen and (max-width: ...