• C#获取网页内容,解决乱码问题


    public static string GetHtml(string url)
            {
                string htmlCode;
                HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                webRequest.Timeout = 30000;
                webRequest.Method = "GET";
                webRequest.UserAgent = "Mozilla/4.0";
                webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");


                HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();

                //获取目标网站的编码格式
                string contentype = webResponse.Headers["Content-Type"];
                Regex regex = new Regex("charset\\s*=\\s*[\\W]?\\s*([\\w-]+)", RegexOptions.IgnoreCase);
                if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
                {
                    using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                    {
                        using (var zipStream = new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                        {

                            //匹配编码格式
                            if (regex.IsMatch(contentype))
                            {
                                Encoding ending = Encoding.GetEncoding(regex.Match(contentype).Groups[1].Value.Trim());
                                using (StreamReader sr = new System.IO.StreamReader(zipStream, ending))
                                {
                                    htmlCode = sr.ReadToEnd();
                                }
                            }
                            else
                            {
                                using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.UTF8))
                                {
                                    htmlCode = sr.ReadToEnd();
                                }
                            }
                        }
                    }
                }
                else
                {
                    using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.Default))
                        {
                            htmlCode = sr.ReadToEnd();
                        }
                    }
                }
                return htmlCode;
            }
    ---------------------
    作者:闪耀的瞬间
    来源:CSDN
    原文:https://blog.csdn.net/zhuyu19911016520/article/details/46647001
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    visual C sharp express from 360 free download
    Druid 在小米公司部分技术实践-博客-云栖社区-阿里云
    公司业务-猫眼知健康
    Sculptor
    可译网 —— 翻译可以更简单
    牛客网-专业IT笔试面试备考平台,最全C++JAVA前端求职题库,全面提升IT编程能力
    设置Redis的LRU策略
    springboot中使用aop技术
    elasticSearch的部署和使用
    jvm原理和代码运行的过程
  • 原文地址:https://www.cnblogs.com/parent/p/9878667.html
Copyright © 2020-2023  润新知