StreamWriter结合UTF-8编码使用不当,会造成BOM(Byte Order Mark )问题生成乱码(转载)
问:
I was using HttpWebRequest to try a rest api in ASP.NET Core MVC.
Here is my HttpWebRequest client code:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:55161/Home/Testing");
string data;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
using (StreamReader reader = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8))
{
data = reader.ReadToEnd();
}
If I used StreamWriter to write a message to Response.Body in an ASP.NET Core controller, everything is fine:
using (var streamWriter = new StreamWriter(Response.Body, System.Text.Encoding.UTF8))
{
streamWriter.Write("hello");
streamWriter.Flush();
}
But if I used Response.Body.Write embedded in a StreamWriter block to write the same message, there will be a weird 65279 character in the end of the string "hello" when I got it from my client code.
using (var streamWriter = new StreamWriter(Response.Body, System.Text.Encoding.UTF8))
{
byte[] data = System.Text.Encoding.UTF8.GetBytes("hello");
Response.Body.Write(data, , data.Length);
}
I want to know if this is a bug or any mechanism caused this problem?
I didn't use UseBrowserLink in startup and my ASP.NET Core version is 2.1
答:
there will be a weird 65279 character in the end of the string "hello" when I got it from my client code
You mean something like this?
hello
This is expected based on the code you provided. Why are you wrapping the stream in a StreamWriter, then writing to the stream directly?
The StreamWriter has a buffer that it will flush to the output when you close it. This will cause a lot of problems if you've been writing to the stream directly. Specifically, what's happening here is this:
- You wrap the Stream in the StreamWriter
- You write hello directly to the stream
- The StreamWriter is closed, so it flushes it's (empty) buffer.
- Since you are using the Encoding.UTF8 encoding, the StreamWriter writes a UTF-8 Byte Order Mark (the sequence 0xEF 0xBB 0xBF which appears as  unless it's at the very beginning of the stream) to the stream. Since you've already written hello, this appears after your hello, causing the rendering glitch above.
因此我们可以看到,在使用StreamWriter的时候,千万不要又用代码直接往StreamWriter底层的Stream对象(本例中是Response.Body)写入数据,因为这很有可能会导致StreamWriter错误地将UTF-8编码的BOM(Byte Order Mark)加到了你写入数据的后面,而UTF-8编码的BOM(Byte Order Mark)只能够出现在一个Stream的最开头才能被正确地识别,否则会被识别为乱码,如同本例中的hello一样。
StreamWriter结合UTF-8编码使用不当,会造成BOM(Byte Order Mark )问题生成乱码(转载)的更多相关文章
- 从Java String实例来理解ANSI、Unicode、BMP、UTF等编码概念
转(http://www.codeceo.com/article/java-string-ansi-unicode-bmp-utf.html#0-tsina-1-10971-397232819ff9a ...
- 编码总结,以及对BOM的理解
一.前言 在跨平台.跨操作系统或者跨区域之间,经常会涉及到编码的问题,因为前段时间在项目中,遇到了因为编码而产生乱码的问题,以前对编码也是一知半解,所以决定对编码有一个更为深入的了解,因此才有了这篇自 ...
- StreamWriter(ms, new UTF8Encoding(false))可以达到不输出BOM的需求。
winform 通过webservice向服务器提交图片需要注意的地方 最近一个winform项目中需要通过拍照或者上传本地文件或者截图的方式把产品图片上传到服务器,最后选择了服务器部署webserv ...
- C#中中文编码的问题(StreamWriter和StreamReader默认编码)
在使用StreamWriter和StreamReader时产生了这样的疑问,在不指定的情况下,他们使用什么编码方式? 查看MSDN,请看下图: 注意红色区域 这让我以为构造函数参数不同时使用不一样的 ...
- 编码占用的字节数 1 byte 8 bit 1 sh 1 bit 中文字符编码 2. 字符与编码在程序中的实现 变长编码 Unicode UTF-8 转换 在网络上传输 保存到磁盘上 bytes
小结: 1.UNICODE 字符集编码的标准有很多种,比如:UTF-8, UTF-7, UTF-16, UnicodeLittle, UnicodeBig 等: 2 服务器->网页 utf-8 ...
- Python核心编程--学习笔记--6--序列(上)字符串
本章研究Python中的序列:字符串.列表和元组.因为这些类型其实都是由一些成员共同组成的一个序列整体,所以我们把它们统称为序列.序列的存储结构可以表示为: 1 序列 序列类型有着相同的访问模式:按下 ...
- pthon核心编程-读书笔记:知识点摘录与总结(方便理解和快速记忆)
Python 中的列表(大小可变的数组)和字典(哈希表)就是内建于语言本身的.在核心语言中提供这些重要的构建单元,可以鼓励人们使用它们, 缩短开发时间与代码量,产生出可读性更好的代码.C不提供, c+ ...
- unicode编码与utf-8 区别
unicode编码与utf-8 区别 如果是为了跨平台兼容性,只需要知道,在 Windows 记事本的语境中: 所谓的「ANSI」指的是对应当前系统 locale 的遗留(legacy)编码.[1] ...
- python的编码问题
本文简单介绍了各种常用的字符编码的特点,并介绍了在python2.x中如何与编码问题作战 :) 请注意本文关于Python的内容仅适用于2.x,3.x中str和unicode有翻天覆地的变化,请查阅其 ...
随机推荐
- Autolayout 01
Auto Layout Concepts auto layout的基本概念是constraint(约束).表示了你interface中的layout元素.例如,你可以创建一个约束来指定元素的宽度或者距 ...
- 解决unknown import path "golang.org/x/sys/unix": unrecognized import path "golang.org/x/sys"
问题描述 当我们使用 go get.go install.go mod 等命令时,会自动下载相应的包或依赖包.但由于众所周知的原因,类似于 golang.org/x/... 的包会出现下载失败的情况. ...
- iis无法启动的解决办法-卸掉KB939373补丁
在本地计算机无法启动 world wide web Publishing 服务错误127:找不到指定的程序 在网上搜索了一下,发现,回答的五花八门, 1.有的说重新安装IIS的,(我重新安装了,还是不 ...
- 防止sql注入和跨站脚本攻击,跨站请求伪造以及一句话木马的学习记录
以下是来自精通脚本黑客的学习记录 防止以上漏洞的最好的方式 一对用户的输入进行编码,对用户输入进行编码,然后存入数据库,取出时解码成utf-8 二对用户的输入进行过滤,过滤jscript,javasc ...
- WinForm启动时接收参数
1 默认的Main函数,修改如下: static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [ST ...
- 知其一不知其二之Jenkins Hacking
转自安全脉搏 本文首发安全脉搏 感谢大王叫我来巡山 的投递 转载请注明来源 大多安全工作者听到jenkins都会知道有个未授权的命令执行 但是如果Script页面要授权才能访问呢 或者你的用户没有Ov ...
- Android Canvas之Path操作
接上篇,Android自己定义View工具:Paint&Canvas(二) 上一篇中介绍的Canvas绘制图形仅仅能画一些常规图形(圆.椭圆.矩形等),假设想绘制更复杂的图形.Path神器来了 ...
- gray-code——找规律
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- mysql导出导入所有数据库
导出所有数据库 mysqldump -uroot -p123456 --all-databases > /home/aa.sql 导入所有数据库 mysql -uroot -p123456 &l ...
- setTimeout()基础/setInterval()基础
JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInterval()这两个函数来完成.它们向任务队列添加定时任务.初始接触它的人都觉得好简单 ...