htmlagilitypack gzip加密例外

c# html-agility-pack

我有异常抛出gzip不支持。这就是我正在使用加载页面,任何关于如何允许gzip的想法?

        HtmlWeb hwObject = new HtmlWeb();
        HtmlAgilityPack.HtmlDocument htmldocObject = hwObject.Load(siteURL);

一般承认的答案

您可以自己下载页面,即使用从WebClient派生的类(或手动创建WebRequest并设置AutomaticDecompression

public class GZipWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        return request;
    }
}

鉴于此,您可以这样做:

string html;
using(var wc = new GZipWebClient())
  html = wc.DownloadString(siteUrl);

var htmldocObject = new HtmlDocument();
htmldocObject.LoadHtml(html);


Related

许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow