C#中将HTML汇总的相对URI更改为绝对URI

80酷酷网    80kuku.com

  

下面的代码用以将给定的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;
}

 

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: