Last edited on 5 December 2008
/*
Resource Qualifier - By Forrest Croce, December 2008.
This code is released as open source; please attribute the author and source.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using HtmlAgilityPack;
namespace FullyQualifyNetworkResourceAddresses {
public static class WebUtility {
public static string GetPageCode(string uri) {
if (!Uri.IsWellFormedUriString(uri, UriKind.Absolute))
return null;
return GetPageCode(new Uri(uri));
}
public static string GetPageCode(Uri uri) {
try {
WebClient web = new WebClient();
return web.DownloadString(uri);
} catch (Exception ex) {
return null;
}
}
public static HtmlDocument GetPage(Uri uri) {
string html = GetPageCode(uri);
if(string.IsNullOrEmpty(html))
return null;
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
return doc;
}
public static Uri Qualify(string baseUri, string relativePath) {
if (Uri.IsWellFormedUriString(relativePath, UriKind.Absolute))
return new Uri(relativePath);
if (!Uri.IsWellFormedUriString(baseUri, UriKind.Absolute))
return null;
Uri b = new Uri(baseUri, UriKind.RelativeOrAbsolute);
return new Uri(b, relativePath);
}
internal static HtmlDocument GetPage(string html) {
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
return doc;
}
}
}