Last post Jul 28, 2015 10:42 AM by Nagaraj Varanasi
Jul 24, 2015 09:29 AM|Nagaraj Varanasi|LINK
I have created one wcf service, request payload in XML format.
Actually payload contains some non english characters like chinese, japanase etc. When ever i send request to another api,(service) in different system, their end these characters are converting into other unreadable format.
example request in payload is 汉语 / 漢語; Hànyǔ or 中文; Zhōngwén) is
other end they are receiving payload as æ±è¯ / æ¼¢èª; HÃ nyÇ or ä¸æ; ZhÅngwÃ©n) is
Can please suggest what could be the reason?
Here is my code :
string hashString = Convert.ToBase64String(hash.ComputeHash(Encoding.Default.GetBytes(xdoc.ToString())));
byte bytes = Encoding.GetEncoding("UTF-8").GetBytes(xdoc.ToString());
request.ContentLength = bytes.Length;
request.ContentType = "application/xml";
Stream requestStream = null;
requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
var streamReader = new StreamReader(responseStream);
Jul 24, 2015 04:31 PM|Rion Williams|LINK
It may be possible that the accepting system (or their API) doesn't accept non-ASCII characters (e.g. Unicode), which many non-English languages use (e.g. Russian, Japanese, Chinese, etc.).
You could also consider using a different Unicode encoding such as UTF-16 or UTF-32 to see if that makes any difference, but if the characters are coming through, it may simply be an issue on their end :
// Simply change the encoding from "UTF-8" to "UTF-16" (or "UTF-32") to see if that has any effect
byte bytes = Encoding.GetEncoding("UTF-16").GetBytes(xdoc.ToString());
Their API may have some specific parameter that needs to be passed in to properly process extended Unicode characters.
Jul 28, 2015 08:27 AM|Nagaraj Varanasi|LINK
I have identified the problem. Problem lies here.
request.ContentType = "application/xml;charset=\"utf-8\"";
After adding charset to contentType its working fine as expected.
Jul 28, 2015 08:30 AM|Rion Williams|LINK
That will do it, I figured that it was simply an issue with the incorrect or a different character set being used. If your issue is resolved, don't forget to mark any answers that were helpful to you using the "Mark as Answer" button.
Jul 28, 2015 10:42 AM|Nagaraj Varanasi|LINK
I marked the above solution as "Mark as Answer"