The intent of the following code is to post data that’s begins with byte order mark (BOM) over HTTP.
var client = new WebClient();
client.Encoding = new UTF8Encoding(true /* encoderShouldEmitUTF8Identifier */);
client.UploadString(url, data);
However according to fiddler there is no BOM at the beginning of the request body. BOM isn’t send even if I use UnicodeEncoding instead of UTF8Encoding.
So question is, what am I doing wrong?
Note: I know that I can bypass this problem by using WebClient.UploadData in combination with Encoding.GetPreamble method however I am wondering why UploadString doesn’t work as I expected.
You’re not doing anything wrong, it’s just that WebClient.UploadString does not call Encoding.GetPreamble – it simply calls
Encoding.GetByteson the string which you passed. On HTTP requests if you’re passing strings you’d usually indicate the encoding in the content-type header (charset parameter), instead of inline in the file (see example below). UploadString does that (it’s tailored for the “common case”). As you mentioned, if you want something extra, you can upload the bytes directly.