htmlagilitypack gzip encryption exception

孤人 提交于 2019-12-03 21:25:37

问题


I'm having the exception throw gzip is not support. This is all i'm using the load the page, any idea on how to allow gzip?

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

回答1:


You can download the page yourself, i.e. using a class derived from WebClient (or manually making a WebRequest and setting 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;
    }
}

Given this you can do:

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

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


来源:https://stackoverflow.com/questions/8936089/htmlagilitypack-gzip-encryption-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!