相对于 HTML 中的绝对路径
我需要通过 URL 创建新闻通讯。为此,我:
- 创建一个
WebClient
。 - 使用WebClient的方法
DownloadData
获取字节数组中的页面源; - 从 source-html 字节数组中获取字符串并将其设置为新闻通讯内容。
但是,我在路径方面遇到了一些麻烦。所有元素的来源都是相对的 (/img/welcome.png
),但我需要一个绝对的来源,例如http://www.example.com/img/welcome.png。
我该怎么做?
I need to create a newsletters by URL. To do that, I:
- Create a
WebClient
. - Use WebClient's method
DownloadData
to get a source of page in byte array; - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试使用 href-attrib = 相关的原始 baseURI 设置基本元素,而不是解析/完成相对路径。
作为标题元素的第一个子元素,所有以下相对路径应由浏览器解析以指向原始目的地,而不是文档(新闻通讯)所在/来自的位置。
在 Firefox 上,所有 src/href-attribs 的获取/设置的一些重言式(<-在形式逻辑中)来回恢复将完整路径写入 html-doc 的所有层(序列化),因此可编写脚本, saveable ...:
当然,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 ...:
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.
如果请求来自您的网站(相同的域链接),那么您可以使用以下命令:
如果您使用的是非 Web 应用程序,或者您想对域名进行硬编码:
if the request comes in from your site (same domain links) then you can use this:
If you're in a non-web app, or you want to hardcode the domain name:
您有一些选择:
这是超级做作的,实际上它的主要部分直接取自: http: //msdn.microsoft.com/en-us/library/xz398a3f.aspx 但它说明了方法 1 背后的基本原理。
You have some options:
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.
只需使用这个功能
Just use this function
解决此任务的可能方法之一是使用 HtmlAgilityPack 库。
一些示例(修复链接):
One of the possible ways to resolve this task is the use the HtmlAgilityPack library.
Some example (fix links):