I recently came across an issue using the WebClient.DownloadString method in System.Net that was causing UTF8 encoded data to not come through properly, and instead was showing odd characters.
Researching the issue led me to the following Stack Overflow articles:
The accepted answers on both work well, so I've put together an example using C# to illustrate the behavior and results.
DownloadString (no encoding specified)
DownloadString (encoding specified)
DownloadData (encoding specified)
Researching the issue led me to the following Stack Overflow articles:
- http://stackoverflow.com/questions/7137165/webclient-downloadstring-results-in-mangled-characters-due-to-encoding-issues-b
- http://stackoverflow.com/questions/4716470/webclient-downloadstring-returns-string-with-peculiar-characters
The accepted answers on both work well, so I've put together an example using C# to illustrate the behavior and results.
DownloadString (no encoding specified)
// variables
string Url = "https://mysafeinfo.com/api/data?list=states&format=json&alias=nm=name,ab=code,cp=capital,yr=year&select=capital&ab=GU";
// DownloadString (no encoding specified)
using (WebClient client = new WebClient())
{
Console.WriteLine(client.DownloadString(Url));
}
// result
[
{
"capital": "Hagåtña Dededo"
}
]
DownloadString (encoding specified)
// variables
string Url = "https://mysafeinfo.com/api/data?list=states&format=json&alias=nm=name,ab=code,cp=capital,yr=year&select=capital&ab=GU";
// DownloadString (encoding specified)
using (WebClient client = new WebClient())
{
// specify encoding
client.Encoding = System.Text.UTF8Encoding.UTF8;
// output
Console.WriteLine(client.DownloadString(Url));
}
// result
[
{
"capital": "Hagåtña Dededo"
}
]
DownloadData (encoding specified)
// variables
string Url = "https://mysafeinfo.com/api/data?list=states&format=json&alias=nm=name,ab=code,cp=capital,yr=year&select=capital&ab=GU";
// DownloadData (encoding specified)
using (WebClient client = new WebClient())
{
Console.WriteLine(System.Text.UTF8Encoding.UTF8.GetString(client.DownloadData(Url)));
}
// result
[
{
"capital": "Hagåtña Dededo"
}
]
One note from the present day: WebClient is obsolete in current versions of .NET, and HttpClient is the supported replacement. The underlying point still holds: GetStringAsync decodes for you and can get the encoding wrong in exactly the same way, while GetByteArrayAsync hands you the raw bytes so you can decode them yourself. If you are hitting the problem described here on modern .NET, the fix is the same, one layer over.