相对于 HTML 中的绝对路径

发布于 2024-08-30 13:40:54 字数 412 浏览 5 评论 0原文

我需要通过 URL 创建新闻通讯。为此,我:

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

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

我该怎么做?

I need to create a newsletters by URL. To do that, I:

  1. Create a WebClient.
  2. Use WebClient's method DownloadData to get a source of page in byte array;
  3. Get string from the source-html byte array and set it to the newsletter content.

However, I have some troubles with paths. All elements' sources were relative (/img/welcome.png) but I need an absolute one, like http://www.example.com/img/welcome.png.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

热风软妹 2024-09-06 13:41:07

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

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

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

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);

当然,url()-func 基数在 STYLE-Element(s, - for background-img 或 content-rules) 以及 style-attrib 的节点级中给出,特别是上述任何解决方案均未考虑/测试 url()-func-stated src/href-values。

因此,让 base-Elem 方法达到有效的、经过测试的(compat-list)状态,对我来说似乎是更有希望的想法。

Instead of resolving/completing relative paths, you can try to set the base-element with the href-attrib = the original baseURI in question.

Placed as the first child of the header-element, all following relative paths should be resolved by browser to point to the original destination, not to where the doc (newsletter) is located/comes from.

on firefox, some tautologic(<-in formal logics) to-and-fro of getting/setting of all src/href-attribs resumes in having COMPLETE paths written to all layers(serialized) of the html-doc, thus scriptable, saveable ...:

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);

Of course, the url()-func bases as given in the STYLE-Element(s, - for background-img or content-rules) as well as in style-attrib's at node-level and in particular the url()-func-stated src/href-values are NOT regarded/tested by any of the solutions above.

Therefore, to get the base-Elem approach to a valid, tested (compat-list) state, seems the more promising notion to me.

寂寞笑我太脆弱 2024-09-06 13:41:04

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

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

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

new Uri("http://www.mysite.com", "/img/welcome.png").ToString();

if the request comes in from your site (same domain links) then you can use this:

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

If you're in a non-web app, or you want to hardcode the domain name:

new Uri("http://www.mysite.com", "/img/welcome.png").ToString();
赠我空喜 2024-09-06 13:41:04

您有一些选择:

  1. 您可以将字节数组转换为字符串并查找替换。
  2. 您可以创建一个 DOM 对象,将字节数组转换为字符串,加载它并将值附加到需要的属性中(基本上您正在寻找其中不包含 http: 或 https: 的任何 src、href 属性)。
    Console.Write(ControlChars.Cr + "Please enter a Url(for example, http://www.msn.com): ")
    Dim remoteUrl As String = Console.ReadLine()
    Dim myWebClient As New WebClient()
    Console.WriteLine(("Downloading " + remoteUrl))
    Dim myDatabuffer As Byte() = myWebClient.DownloadData(remoteUrl)
    Dim download As String = Encoding.ASCII.GetString(myDataBuffer)
    download.Replace("src=""/", "src=""" & remoteUrl & "/")
    download.Replace("href=""/", "href=""" & remoteUrl & "/")
    Console.WriteLine(download)
    Console.WriteLine("Download successful.")

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

You have some options:

  1. You can convert your byte array to a string and find replace.
  2. You can create a DOM object, convert the byte array to string, load it and append the value to the attributes where needed (basically you are looking for any src, href attribute that doesn't have http: or https: in it).
    Console.Write(ControlChars.Cr + "Please enter a Url(for example, http://www.msn.com): ")
    Dim remoteUrl As String = Console.ReadLine()
    Dim myWebClient As New WebClient()
    Console.WriteLine(("Downloading " + remoteUrl))
    Dim myDatabuffer As Byte() = myWebClient.DownloadData(remoteUrl)
    Dim download As String = Encoding.ASCII.GetString(myDataBuffer)
    download.Replace("src=""/", "src=""" & remoteUrl & "/")
    download.Replace("href=""/", "href=""" & remoteUrl & "/")
    Console.WriteLine(download)
    Console.WriteLine("Download successful.")

This is super contrived and actually the main brunt of it is taken directly from : http://msdn.microsoft.com/en-us/library/xz398a3f.aspx but it illustrates the basic principal behind method 1.

财迷小姐 2024-09-06 13:41:04

只需使用这个功能

'# 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

Just use this function

'# 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
情域 2024-09-06 13:41:03

解决此任务的可能方法之一是使用 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);
    }
}

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

Some example (fix links):

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文