3

I am using below code snippet to download HTTP response to local file. Sometimes my content which is in url is multi-lingual (chinese, japanese, thai data etc.). I am using ContentEncoding header to specify my content is in UTF-8 encoding, but this has no effect in my local output file which is generating in ASCII. Due to this, multi-lingual data is corrupted. Any help?

using (var webClient = new WebClient())
        {
            webClient.Credentials = CredentialCache.DefaultCredentials;
            webClient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0");
            webClient.Headers.Add(HttpRequestHeader.ContentEncoding, "utf-8");

            webClient.DownloadFile(url, @"c:\temp\tempfile.htm");
        }
2
  • If you want to support wide characters you should use something like utf-16 Commented Jul 17, 2012 at 22:07
  • 1
    UTF8 is not ASCII/ANSI, but UTF8 supports ASCII character set. Commented Jul 17, 2012 at 23:00

1 Answer 1

7

The ContentEncoding header is not used to specify the character set. It's used by the client to say what kind of encoding (compression) it supports.

The client can't tell the server what character set to send. The server sends its data and some header fields that say what character set is being used. Typically it's in the ContentTypeheader and looks like: text/html; charset=UTF-8.

When you're using WebClient, you want to set the Encoding property as a fallback so that if the server doesn't identify the character set, your default will be used. For example:

WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
string s = client.DownloadString(DownloadUrl);

See http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=800 for a bit more information.

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

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.