天天看點

c#抓取頁面亂碼解決辦法

public static string GetHtmlWithUtf(string url)  
    {  
        if (!(url.Contains("http://") || url.Contains("https://")))  
        {  
            url = "http://" + url;  
        }  
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);  
        req.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";  
        req.Accept = "*/*";  
        req.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");  
        req.ContentType = "text/xml";  
  
        string sHTML = string.Empty;  
        using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())  
        {  
            if (response.ContentEncoding.ToLower().Contains("gzip"))  
            {  
                using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))  
                {  
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))  
                    {  
                        sHTML = reader.ReadToEnd();  
                    }  
                }  
            }  
            else if (response.ContentEncoding.ToLower().Contains("deflate"))  
            {  
                using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress))  
                {  
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))  
                    {  
                        sHTML = reader.ReadToEnd();  
                    }  
                }  
            }  
            else  
            {  
                using (Stream stream = response.GetResponseStream())  
                {  
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))  
                    {  
                        sHTML = reader.ReadToEnd();  
                    }  
                }  
            }  
        }  
        return sHTML;  
    }
           

轉自:https://blog.csdn.net/daiyan_csdn/article/details/52224667