下面的代码用以将给定的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;
}
新闻热点
疑难解答