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有翻天覆地的变化,请查阅其 ...
随机推荐
- C# 基础练习题
(一)循环 1.打印输出 5 个"Hello world!" ////输出 5 个"Hello world!" for (int i = 0; i < 5 ...
- 【IntelliJ IDEA】Debug调试的使用记录
Debug调试是否能行云流水的使用,是鉴定你会不会使用一个IDE的最基本的标准. 今天再这里把Idea的debug使用流程走一遍. ================================== ...
- DEDECMS图片集上传图片出错302的解决办法
无忧主机(www.51php.com)小编今天在调试dede网站的时候发现了一个问题,因为小编想在网站上增加一个图片集的栏目,于是就到后台图片集栏目去添加内容,谁知在上传图片的时候给我弹出个错误信息框 ...
- Elite Container DELPHI下的一个轻量级IoC对象容器
一.简介: Elite Container是DELPHI下的一个轻量级IoC对象容器(IoC:Inverse of Control,反转控制).它是参考了Java中的Spring框架(主要是配置文件的 ...
- 将ActiveX控件标记为安全
參考网页 http://msdn.microsoft.com/en-us/library/aa751977(v=vs.85).aspx http://support.microsoft.com/kb/ ...
- 从有序数组中查找某个值 low_bound
二分搜索 题意: 给定长度为n的单调不下降数列a0, ...an-1和一个数k,求满足ai>=k条件的最小的i. 不存在的情况下输出n. 输入: 5 3 2 3 3 5 6 输出: ...
- 重装系统后恢复wubi安装的Ubuntu(未实测)
wubi安装成功,但是后来windows系统重装了,如何修复ubuntu系统的引导?[另外完全可以复制别人的wubi安装的ubuntu,但是要放在同一个盘符下] 将X:/ubuntu/winboo ...
- DisplayPort的时钟隐藏和时钟恢复
转:DisplayPort的时钟隐藏和时钟恢复 无时钟线的视频数据传输是DP协议的一大特点,将时钟信号隐藏在数据中是传输协议的设计趋势.时钟恢复技术也是DP芯片设计的关键技术.在这说一下在发送端时钟是 ...
- getOutString 输出弹出字符串
输入字符串长度,字符串,计数m.从前往后计数,当数到m个元素时,第m个元素出列,同时将该元素赋值给m,然后从下一个数计数循环,直到所有数字都出列,给定的数全部为大于0的数字.输出出队队列. 例如: ...
- mysql索引类型normal,unique,full text
normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用作索引时,可设置为unique full textl: 表示 全文搜索的索引. FULL ...