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.

Jenix
  • 2,996
  • 2
  • 29
  • 58
  • 1
    Is this an answer to your question? https://stackoverflow.com/a/4414118/2395747 – wmioduszewski Oct 26 '17 at 13:21
  • 2
    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.ContentEncoding – Hans Passant Oct 26 '17 at 13:24
  • @wmioduszewski Thanks, that answer also helped! – Jenix Oct 26 '17 at 13:24
  • @HansPassant Thank you thank you! :) – Jenix Oct 26 '17 at 13:25

1 Answers1

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.

Elanis
  • 184
  • 3
  • 12
MarkO
  • 2,143
  • 12
  • 14