我有异常抛出gzip不支持。这就是我正在使用加载页面的任何想法,如何允许gzip?
HtmlWeb hwObject = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldocObject = hwObject.Load(siteURL);
答案 0 :(得分:11)
您可以自行下载该页面,即使用从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);