0

I've got this string returned via HTTP Post from a URL in a C# application, that contains some chinese character eg:

Gelatos® Colors Gift Set中文

Problem is I want to convert it to

Gelatos® Colors Gift Set中文

Both string are actually identical but encoded differently. I understand in C# everything is UTF16. I've tried reading alof of postings here regarding converting from one encoding to the other but no luck.

Hope someone could help.

Here's the C# code:

WebClient wc = new WebClient();
json = wc.DownloadString("http://mysite.com/ext/export.asp");

textBox2.Text = "Receiving orders....";

//convert the string to UTF16
        Encoding ascii = Encoding.ASCII;
        Encoding unicode = Encoding.Unicode;
        Encoding utf8 = Encoding.UTF8;

        byte[] asciiBytes = ascii.GetBytes(json);
        byte[] utf8Bytes = utf8.GetBytes(json);
        byte[] unicodeBytes = Encoding.Convert(utf8, unicode, utf8Bytes);

        string sOut = unicode.GetString(unicodeBytes);

System.Windows.Forms.MessageBox.Show(sOut);  //doesn't work...

Here's the code from the server:

<%@CodePage = 65001%>
<%option explicit%>
<%
Session.CodePage = 65001
Response.charset ="utf-8"
Session.LCID     = 1033 'en-US

..... response.write (strJSON)

%>

The output from the web is correct. But I was just wondering if some changes is done on the http stream to the C# application.

thanks.

2
  • What conversion functions have you tried, and whats goes wrong? Commented Jul 10, 2013 at 8:50
  • Yip: Could you show us the code responsible for the HTTP POST that generates the string? Commented Jul 10, 2013 at 8:52

2 Answers 2

1

Download the web pages as bytes in the first place. Then, convert the bytes to the correct encoding.

By first converting it using a wrong encoding you are probably losing data. Especially using ASCII.

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

2 Comments

thanks, but how do you download from a URL using a specific encoding?
Ok, got the download part using byte[] response = new System.Net.WebClient().DownloadData(url);
0

If the server is really returning UTF-8 text, you can configure your WebClient by setting its Encoding property. This would eliminate any need for subsequent conversions.

using (WebClient wc = new WebClient())
{
    wc.Encoding = Encoding.UTF8;
    json = wc.DownloadString("http://mysite.com/ext/export.asp");
}

1 Comment

Thanks Douglas....Finally get this right. The ASP does return UTF8, and the C# will thus display the right string returned.

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.