黃聰:C#中WebClient自動判斷編碼是UTF-8還是GBK,並且有超時判斷功能


public class WebDownload : WebClient
    {
        private int _timeout;
        /// <summary>
        /// 超時時間(毫秒)
        /// </summary>
        public int Timeout
        {
            get
            {
                return _timeout;
            }
            set
            {
                _timeout = value;
            }
        }

        public WebDownload()
        {
            this._timeout = 60000;
        }

        public WebDownload(int timeout)
        {
            this._timeout = timeout;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            var result = base.GetWebRequest(address);
            result.Timeout = this._timeout;
            return result;
        }

        public string Get(string url)
        {
            string html = "";
            var data = this.DownloadData(url);
            var r_utf8 = new System.IO.StreamReader(new System.IO.MemoryStream(data), Encoding.UTF8); //將html放到utf8編碼的StreamReader內
            var r_gbk = new System.IO.StreamReader(new System.IO.MemoryStream(data), Encoding.Default); //將html放到gbk編碼的StreamReader內
            var t_utf8 = r_utf8.ReadToEnd(); //讀出html內容
            var t_gbk = r_gbk.ReadToEnd(); //讀出html內容
            if (!isLuan(t_utf8)) //判斷utf8是否有亂碼
            {
                html = t_utf8;
            }
            else
            {
                html = t_gbk;
            }

            return html;
        }

        bool isLuan(string txt)
        {
            var bytes = Encoding.UTF8.GetBytes(txt);
            //239 191 189
            for (var i = 0; i < bytes.Length; i++)
            {
                if (i < bytes.Length - 3)
                    if (bytes[i] == 239 && bytes[i + 1] == 191 && bytes[i + 2] == 189)
                    {
                        return true;
                    }
            }
            return false;
        }

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM