3

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
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 for more.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.