2

I'm new to Windows phone 8 Dev and C#. I try to get String from remote server. This is my code:

  var client = new HttpClient();
  var response = await client.GetAsync(MyUrl);
  string charset = GetCharset(response.Content.Headers.ContentType.ToString());
  Debug.WriteLine("charset: " + charset); \\debug show: charset: ISO-8859-1
  var bytesCharset = await response.Content.ReadAsByteArrayAsync();
  Debug.WriteLine(Encoding.UTF8.GetString(bytesCharset,0,bytesCharset.Length)); 
  //Debug show: `m\u1EF9 t�m`

m\u1EF9 t�m is what I got, but I expect it's Vietnamese: Mỹ Tâm. Can anyone give me an idea to get expected result? (I think the bytes is encoded in IOS-8859-1).

3 Answers 3

2

Encoding.UTF8 should be interpreting your data correctly, as ISO-8859-1 is a subset of UTF-8. However, so that other encodings are handled transparently, it is probably better to use HttpContent's ReadAsStringAsync() method instead of ReadAsByteArrayAsync().

I suspect you have the correct string there but your debug window simply cannot display those two unicode characters properly.

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

1 Comment

I used ReadAsStringAsync() in real device, but I show the same results as debug.
0

You can use the static GetEncoding method to request a specificy codepage. e.g.:

var encoding = Encoding.GetEncoding("ISO-8859-1");

Comments

0

Finally I found the solution. After using Json.Net to parse json from remote server, my problem's gone, and I can get expected result:

 JArray jArray = JArray.Parse(isoText);
 String exptectedText= jArray[1].ToString();

Actually, I dont know why my code works :( Thank you anyway!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.