HtmlAgilityPack HtmlWeb.Load() 不支持 gzip 的解决方法
2017-07-30 18:13
573 查看
这几天想做用现在比较流行的HtmlAgilityPack为雨泽漫画重写下采集的功能,看了一些HtmlAgilityPack 的介绍,感觉非常好用,还内置了htmlWeb用来http请求。但是发现有的开启了gzip压缩的网页请求时会报错。
原来的代码如下:
报错信息为“gzip”不是受支持的编码名。
在谷歌上搜索了半天,终于找到解决方案,而且不必更换HttpRequest或WebClient进行请求。同时还可以用此方法设置cookie,render伪装等等。。。
解决后代码如下:
view source
print?
原来的代码如下:
1 | HtmlWeb webClient = new HtmlWeb(); |
2 | HtmlDocument doc = webClient.Load( this .getUrl()); |
在谷歌上搜索了半天,终于找到解决方案,而且不必更换HttpRequest或WebClient进行请求。同时还可以用此方法设置cookie,render伪装等等。。。
解决后代码如下:
view source
print?
1 | HtmlWeb webClient = new HtmlWeb(); |
2 | HtmlAgilityPack.HtmlWeb.PreRequestHandler handler = delegate (HttpWebRequest request) |
3 | { |
4 | request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate" ; |
5 | request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; |
6 | request.CookieContainer = new System.Net.CookieContainer(); |
7 | return true ; |
8 | }; |
9 | webClient.PreRequest += handler; |
10 | HtmlDocument doc = webClient.Load( this .getUrl()); |
相关文章推荐
- HtmlAgilityPack HtmlWeb.Load() 不支持 gzip 的解决方法
- HtmlAgilityPack不能解析<select><option>的解决方法
- 解决HttpWebRequest和HtmlAgilityPack采集网页中文乱码问题
- 使用HtmlAgilityPack和ScrapySharp抓取网页数据遇到的几个问题解决方法——格式编码问题
- HtmlAgilityPack不能解析option标签的解决方法
- Ajax.net 错误 Could not load type 'Microsoft.Web.Extensions.Design.Dll'解决方法.
- 转:eWebEditor不支持IE8的解决方法[js报错:缺少对象 editor.js]
- 真正解决HtmlAgilityPack抓取网页 中文乱码问题
- eWebEditor 不支持ie7、ie8的解决方法
- 解决HtmlAgilityPack中文乱码
- IE6-IE9不支持table.innerHTML的解决方法分享
- eWebEditor不支持IE8的解决方法[js报错:缺少或不支持对象 editor.js]
- WPF 使用 .net framework 4.0 client profile 不能引用system.web 解决方法(UrlEncode,HtmlEncode)
- myEclipse下web.xml不支持标记的解决方法
- eWebEditor不支持IE8IE7火狐遨游的解决方法
- eWebEditor不支持IE8的解决方法[js报错:缺少对象 editor.js]
- 真正解决HtmlAgilityPack抓取网页 中文乱码问题
- tapestry不支持 等html特殊符号的解决方法
- webview加载htmlstring里有工程里的图片解决方法
- Parser Error Message: Could not load file or assembly 'Telerik.Web.UI' or one of its dependencies.解决方法