Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6051587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:50:06+00:00 2026-05-23T07:50:06+00:00

The intent of the following code is to post data that’s begins with byte

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:50:07+00:00Added an answer on May 23, 2026 at 7:50 am

    You’re not doing anything wrong, it’s just that WebClient.UploadString does not call Encoding.GetPreamble – it simply calls Encoding.GetBytes on 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.

    public class StackOverflow_5731102
    {
        [ServiceContract]
        public class Service
        {
            [WebInvoke]
            public Stream Process(Stream input)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var header in WebOperationContext.Current.IncomingRequest.Headers.AllKeys)
                {
                    sb.AppendLine(string.Format("{0}: {1}", header, WebOperationContext.Current.IncomingRequest.Headers[header]));
                }
    
                string contentType = WebOperationContext.Current.IncomingRequest.ContentType;
                Encoding encoding = Encoding.GetEncoding(contentType.Substring(contentType.IndexOf('=') + 1));
                WebOperationContext.Current.OutgoingResponse.ContentType = WebOperationContext.Current.IncomingRequest.ContentType;
                return new MemoryStream(encoding.GetBytes(sb.ToString()));
            }
        }
    
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
            host.Open();
            Console.WriteLine("Host opened");
    
            foreach (var encoding in new Encoding[] { new UTF8Encoding(true), new UnicodeEncoding(false, true) })
            {
                Console.WriteLine("Sending encoding = {0}", encoding.WebName);
                WebClient client = new WebClient();
                client.Headers[HttpRequestHeader.ContentType] = "text/plain; charset=" + encoding.WebName;
                client.Encoding = encoding;
                string url = baseAddress + "/Process";
                string data = "hello";
                string result = client.UploadString(url, data);
                Console.WriteLine(result);
    
                Console.WriteLine(string.Join(",", encoding.GetBytes(data).Select(b => b.ToString("X2"))));
            }
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jQuery code: $.ajax({ type: POST, url: /search, data: $(form).serialize(), success:
If I have the following code: Intent intent = new Intent(this,DownloadService.class); for(int i=0;i<filesArray.length;i++){ startService(intent);
I'm using following code to open a gallery inside of my app Intent intent
i have the following code that lists all the jpeg in the phone's gallery.
I am using following code to get data from the Server and if the
I have a main activity that launches a child activity using the following code:
I am trying to run the following recognizer intent example that i have found
I have the following code: Intent myIntent = new Intent(Intent.ACTION_VIEW, ContentURI.create(arAdapter.getItem(position).getUrl())); startActivity(myIntent); But I
I am creating a repeating alarm. This works fine using the following code: Intent
I'm creating a notification with something similar to the following: Intent ni = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.