Friday, April 17, 2015

3 Ways to Load Html with HtmlAgilityPack

Method 1:
var web = new HtmlWeb();
var doc = web.Load(sourceUrl);

Method 2:
var uri = new Uri(sourceUrl);
var request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
var response = (HttpWebResponse)request.GetResponse();

var doc = new HtmlDocument();
var stream = response.GetResponseStream();
doc.Load(stream, Encoding.UTF8);

Method 3:
using (var wc = new WebClient()){
html = wc.DownloadString(sourceUrl);
}
var doc = new HtmlDocument();
doc.LoadHtml(html);

No comments: