WeClient 抓取網頁內容是亂碼

C#、WebClient、亂碼

使用WebClient 的DownloadString下載網頁時,下載回來的網頁是一堆亂碼,

就算Encoding改成UTF-8也一樣

用Fiddler欄封包看,發現內容被用GZIP壓縮過了

Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 6938
Content-Type: text/html;charset=UTF-8

       < n u C@ P;      :á@Q .c-E/  
A t ̔ط  pvW@?  / C ;F       - ̗䜺 e.  0 ; ] S :u.u[YQ;     2枻   r ~ +

*** FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable truncation. ***

此時要修改WeClient複寫讓下載的內容自動解壓縮

public class AutoDecompressWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri uri)
    {
        WebRequest request = base.GetWebRequest(uri);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        }
        return request;
    }
}