Does Encoding.UTF8.GetBytes() create a BOM?
Asked Answered
H

1

6

I'm making an HTTP POST request with this:

byte[] postBuffer = Encoding.UTF8.GetBytes(postStr);

So far, this seems working fine but I'm not sure if this will always work, because Encoding.UTF8 means UTF8 WITH BOM. When I create local files with StreamWriter, always use the default encoding which is the same as new UTF8Encoding(false) in order to write WITHOUT BOM. So wonder if the same is true for calling GetBytes() method too.

In this case, isn't there any difference between above and below line?

byte[] postBuffer = new UTF8Encoding().GetBytes(postStr);

I tested several times myself but still not sure 100%, so asking here.

Hornstein answered 26/10, 2017 at 13:5 Comment(4)
Is this an answer to your question? https://mcmap.net/q/56904/-how-to-getbytes-in-c-with-utf8-encoding-with-bomZebedee
That's not what it means. On Windows a BOM is pretty important for a file to make apps that don't know anything about the file still interpret it correctly. Just not the case for a network protocol since both ends need to agree about the stream content. And for http the header dictates the encoding. You'd rely on, say, HttpWebResponse.ContentEncodingPazice
@Zebedee Thanks, that answer also helped!Hornstein
@HansPassant Thank you thank you! :)Hornstein
H
5

The GetBytes method never returns a stream of bytes prefaced with a BOM. The BOM can be retrieve by using the GetPreAmble method, which returns nothing when the encoder is instantiated with false.

See the extensive Remarks section on Microsoft Documentation for more details.

Hanghangar answered 26/10, 2017 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.