抓取utf-8页面乱码的解决办法

来源:互联网 发布:阿里云 cname 配置 编辑:程序博客网 时间:2024/06/05 09:25

        public string GetPageData(string URL)
        {
            Regex utf = new Regex(@"[cC][hH][aA][rR][sS][eE][tT] *= *'?[uU][tT][fF]-8");
            try
            {
                WebClient client = new WebClient();
                client.Encoding = Encoding.Default;
                string input = client.DownloadString(URL);
                Match m = utf.Match(input);
                if (m.Success)
                {
                    client.Encoding = Encoding.UTF8;
                    input = client.DownloadString(URL);
                    return input;
                }
                else
                    return input;
            }
            catch (Exception)
            {
                MessageBox.Show("获取数据失败(1)!");
                return null;
            }
        }

原创粉丝点击