相对于绝对路径的HTML(asp.net) [英] Relative to absolute paths in HTML (asp.net)

查看:193
本文介绍了相对于绝对路径的HTML(asp.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过URL来创建一个通讯。我接下来要做的:

I need to create a newsletters by URL. I to do next:


  1. 创建一个Web客户端;

  2. 使用Web客户端的方法
    DownloadData获得页面的源
    在字节数组;

  3. 获取从源代码的HTML字节的字符串
    数组,并将其设置电子报
    内容。

但我有一些麻烦与路径。所有元素的来源是相对的( /img/welcome.png 的),但我需要绝对的( http://www.mysite.com/img/welcome.png 的)。

But I have some troubles with paths. All elements' sources were relative (/img/welcome.png) but I need absolute (http://www.mysite.com/img/welcome.png).

我怎样才能做到这一点?

How can I do this?

最好的问候,亚历克斯。

Best regards, Alex.

推荐答案

一来解决这个任务可能的方式是利用 HtmlAgilityPack 库。

One of the possible ways to resolve this task is the use the HtmlAgilityPack library.

一些示例(修复链接的):

WebClient client = new WebClient();
byte[] requestHTML = client.DownloadData(sourceUrl);
string sourceHTML = new UTF8Encoding().GetString(requestHTML);

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(sourceHTML);

foreach (HtmlNode link in htmlDoc.DocumentNode.SelectNodes("//a[@href]"))
{
    if (!string.IsNullOrEmpty(link.Attributes["href"].Value))
    {
        HtmlAttribute att = link.Attributes["href"];
        att.Value = this.AbsoluteUrlByRelative(att.Value);
    }
}

这篇关于相对于绝对路径的HTML(asp.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆