#if METRO
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace HtmlAgilityPack
{
///
/// Used for downloading and parsing html from the internet
///
public class HtmlWeb
{
///
/// Allows for setting document defaults before loading
///
public Action PreHandleDocument { get; set; }
#region Instance Methods
///
/// Begins the process of downloading an internet resource
///
/// Url to the html document
public async Task LoadFromWebAsync(string url)
{
return await LoadFromWebAsync(new Uri(url), null, null);
}
///
/// Begins the process of downloading an internet resource
///
/// Url to the html document
/// The encoding to use while downloading the document
public async Task LoadFromWebAsync(string url, Encoding encoding)
{
return await LoadFromWebAsync(new Uri(url), encoding, null);
}
///
/// Begins the process of downloading an internet resource
///
/// Url to the html document
/// The encoding to use while downloading the document
/// Username to use for credentials in the web request
/// Password to use for credentials in the web request
public async Task LoadFromWebAsync(string url, Encoding encoding, string userName, string password)
{
return await LoadFromWebAsync(new Uri(url), encoding, new NetworkCredential(userName, password));
}
///
/// Begins the process of downloading an internet resource
///
/// Url to the html document
/// The encoding to use while downloading the document
/// Username to use for credentials in the web request
/// Password to use for credentials in the web request
/// Domain to use for credentials in the web request
public async Task LoadFromWebAsync(string url, Encoding encoding, string userName, string password, string domain)
{
return await LoadFromWebAsync(new Uri(url), encoding, new NetworkCredential(userName, password, domain));
}
///
/// Begins the process of downloading an internet resource
///
/// Url to the html document
/// Username to use for credentials in the web request
/// Password to use for credentials in the web request
/// Domain to use for credentials in the web request
public async Task LoadFromWebAsync(string url, string userName, string password, string domain)
{
return await LoadFromWebAsync(new Uri(url), null, new NetworkCredential(userName, password, domain));
}
///
/// Begins the process of downloading an internet resource
///
/// Url to the html document
/// Username to use for credentials in the web request
/// Password to use for credentials in the web request
public async Task LoadFromWebAsync(string url, string userName, string password)
{
return await LoadFromWebAsync(new Uri(url), null, new NetworkCredential(userName, password));
}
///
/// Begins the process of downloading an internet resource
///
/// Url to the html document
/// The credentials to use for authenticating the web request
public async Task LoadFromWebAsync(string url, NetworkCredential credentials)
{
return await LoadFromWebAsync(new Uri(url), null, credentials);
}
///
/// Begins the process of downloading an internet resource
///
/// Url to the html document
/// The encoding to use while downloading the document
/// The credentials to use for authenticating the web request
public async Task LoadFromWebAsync(Uri uri, Encoding encoding, NetworkCredential credentials)
{
var clientHandler = new HttpClientHandler();
if (credentials == null)
clientHandler.UseDefaultCredentials = true;
else
clientHandler.Credentials = credentials;
var client = new HttpClient(clientHandler);
var e = await client.GetAsync(uri);
if (e.StatusCode == HttpStatusCode.OK)
{
var html = string.Empty;
if (encoding != null)
{
using (var sr = new StreamReader(await e.Content.ReadAsStreamAsync(), encoding))
{
html = sr.ReadToEnd();
}
}
else
html = await e.Content.ReadAsStringAsync();
var doc = new HtmlDocument();
if (PreHandleDocument != null)
PreHandleDocument(doc);
doc.LoadHtml(html);
return doc;
}
throw new Exception("Error downloading html");
}
#endregion
}
}
#endif