2

I have the below code in a webservice to read the post data. The issue here is whenever the request contains special character, lets say "amé", the character is replaced as am� while converted to string.

 byte[] postData= HttpContext.Request.BinaryRead(HttpContext.Request.ContentLength);
 string strReq = Encoding.UTF8.GetString(postData);

And I call the WebService with the below code:

WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "text/xml; charset=utf-8";
webClient.Headers[HttpRequestHeader.Authorization] = credentials;
string output = webClient.UploadString(url, "POST", input);

1 Answers1

1

You'll need to specify in your web service that the post data is UTF-8 encoded, otherwise you won't be able to decode it as UTF-8. Adding charset=utf-8 to the end of your content-type header should do the trick.

e.g.

'content-type': 'text/xml; charset=utf-8'

Note that specifications for JSON and form encoded POST data require UTF-8.

Mourndark
  • 2,526
  • 5
  • 28
  • 52
  • Thanx Mourndark.. I was able to decode the post data with the above code, except that the special character in post data like é is not getting reflected in the decoded string – Aarthi Priyadharshini Jul 01 '16 at 10:19
  • Yes, regular Latin characters encoded in ASCII will decode fine in UTF-8 so it's no guarantee. I had a closer look at �, it's U+FFFD, the Unicode Replacement Character that is inserted whenever an invalid character is found. See [this answer](http://stackoverflow.com/a/15022396/1096201) for more. – Mourndark Jul 01 '16 at 10:36