7

我需要通过 URL 创建时事通讯。为此,我:

  1. 创建一个WebClient.
  2. 使用WebClient的方法DownloadData获取字节数组中的页面源;
  3. 从 source-html 字节数组中获取字符串并将其设置为时事通讯内容。

但是,我在路径方面遇到了一些麻烦。所有元素的来源都是相对的(/img/welcome.png),但我需要一个绝对的来源,例如http://www.example.com/img/welcome.png

我怎样才能做到这一点?

4

5 回答 5

6

解决此任务的一种可能方法是使用HtmlAgilityPack库。

一些例子(修复链接):

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);
    }
}
于 2010-04-27T08:23:13.730 回答
2

如果请求来自您的站点(相同的域链接),那么您可以使用它:

new Uri(Request.Uri, "/img/welcome.png").ToString();

如果您使用的是非网络应用程序,或者您想对域名进行硬编码:

new Uri("http://www.mysite.com", "/img/welcome.png").ToString();
于 2010-04-27T07:34:11.017 回答
0

你有一些选择:

  1. 您可以将字节数组转换为字符串并查找替换。
  2. 您可以创建一个 DOM 对象,将字节数组转换为字符串,加载它并将值附加到需要的属性中(基本上您正在寻找任何没有 http: 或 https: 的 src、href 属性)。
    Console.Write(ControlChars.Cr + "请输入一个Url(例如http://www.msn.com):")
    将 remoteUrl 调暗为 String = Console.ReadLine()
    将 myWebClient 调暗为新 WebClient()
    Console.WriteLine(("正在下载" + remoteUrl))
    将 myDatabuffer 调暗为 Byte() = myWebClient.DownloadData(remoteUrl)
    暗淡下载 As String = Encoding.ASCII.GetString(myDataBuffer)
    download.Replace("src=""/", "src=""" & remoteUrl & "/")
    download.Replace("href=""/", "href=""" & remoteUrl & "/")
    Console.WriteLine(下载)
    Console.WriteLine("下载成功。")

这是超级做作的,实际上它的主要冲击直接来自: http: //msdn.microsoft.com/en-us/library/xz398a3f.aspx,但它说明了方法 1 背后的基本原理。

于 2010-04-27T07:32:52.787 回答
0

只需使用此功能

'# converts relative URL ro Absolute URI
    Function RelativeToAbsoluteUrl(ByVal baseURI As Uri, ByVal RelativeUrl As String) As Uri
        ' get action tags, relative or absolute
        Dim uriReturn As Uri = New Uri(RelativeUrl, UriKind.RelativeOrAbsolute)
        ' Make it absolute if it's relative
        If Not uriReturn.IsAbsoluteUri Then
            Dim baseUrl As Uri = baseURI
            uriReturn = New Uri(baseUrl, uriReturn)
        End If
        Return uriReturn
    End Function
于 2011-08-20T21:19:11.777 回答
0

您可以尝试使用 href-attrib = 有问题的原始 baseURI 设置基本元素,而不是解析/完成相对路径。

作为标题元素的第一个子元素,浏览器应解析所有以下相对路径以指向原始目的地,而不是文档(时事通讯)所在/来自的位置。

在 Firefox 上,所有 src/href-attribs 的获取/设置的一些重言式(<-informal logics)来回恢复将完整路径写入 html-doc 的所有层(序列化),因此可编写脚本,可保存...:

var d=document;
var n= d.querySelectorAll('[src]'); // do the same for [href] ...
var i=0; var op ="";var ops="";
for (i=0;i<n.length;i++){op = op + n[i].src + "\n";ops=n[i].src;
n[i].src=ops;}
alert(op);

当然,在 STYLE-Element(s, - for background-img 或 content-rules) 以及在节点级别的样式属性中给出的 url()-func 基础,特别是 url()-func - 声明的 src/href-values 不被上述任何解决方案考虑/测试。

因此,让 base-Elem 方法进入一个有效的、经过测试的(兼容列表)状态,对我来说似乎是更有希望的想法。

于 2013-08-27T01:52:46.597 回答