C webclient Chinese Garbled Code Problem Solution

  • 2021-10-11 19:19:45
  • OfStack

When webclient calls DownloadData or DownloadString, the data requested back has garbled problem. The solution is as follows:

1. Set the encoding format of webclient as the target encoding format

WebClient web = new WebClient();// Create webclient Object 
web.Encoding = System.Text.Encoding.UTF8;// Define object language
string returns = web.DownloadString("_http://www.weather.com.cn/data/sk/101310101.html");// Toward 1 Connection request resources

2. Get the data first and then transcode it

WebClient wc = new WebClient();
Byte[] pageData = wc.DownloadData("http://m.weather.com.cn/data/101110101.html");
string rr = Encoding.GetEncoding("utf-8").GetString(pageData);

To sum up, it is still a coding problem. No matter which method, you can set up the coding.


Related articles: