下面的代碼用以將給定的Html中的所有的URI,包括href和img都更改為絕對路徑
private static string ConvertToAbsoluteUrls (string Html, Uri relativeLocation) {
IHTMLDocument2 doc = new HtmlDocumentClass ();
doc.write (new object [] { Html });
doc.close ();
foreach (IHtmlAnchorElement anchor in doc.links) {
IHTMLElement element = (IHtmlElement)anchor;
string href = (string)element.getAttribute ("href", 2);
if (href != null) {
Uri addr = new Uri (relativeLocation, href);
anchor.href = addr.AbsoluteUri;
}
}
foreach (IHtmlImgElement image in doc.images) {
IHTMLElement element = (IHtmlElement)image;
string src = (string)element.getAttribute ("src", 2);
if (src != null) {
Uri addr = new Uri (relativeLocation, src);
image.src = addr.AbsoluteUri;
}
}
string ret = doc.body.innerHtml;
return ret;
}