English 中文(简体)
相对于超文本绝对途径
原标题:Relative to absolute paths in HTML

我需要由URL创建通讯。 为此,我:

  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.

然而,我对道路有一些麻烦。 所有来源都是相对的(/img/welcome.png),但我需要绝对的,例如

我如何能够这样做?

最佳回答

解决这一问题的可能途径之一是使用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);
    }
}
问题回答

如果请求来自贵网站(同一领域链接),你可以使用:

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

如果你用非网站浏览,或想硬写域名:

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

你有一些选择:

  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.")

它是表面上的,实际上是从以下网址直接提取的:。 但它说明了方法背后的基本主要原理。 1. 导言

公正利用这一职能

 # 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

您不能解决/完成相对的道路,而是可以尝试将基础内容与Rhref-attrib = 有关的原始基础。

作为头盔的首批儿童,所有走过相对道路的人都应由浏览器解决,以指明原目的地,而不是指代号(通讯)位于何处/来自何处。

on英,一些ta(加固;正式逻辑)到--fro,所有rc/幼att都重新开始采用孔径终端向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 + "
";ops=n[i].src;
n[i].src=ops;}
alert(op);

当然,STYLE-Element(s)中给出的尿素(func)基础,即背景或内容规则,以及在节一级,特别是在陶尔()-func-stated src/href- Values上,都由上述任何解决办法看待/测试。

因此,为了对一个有效、经过测试(比较清单)的国家采取基-埃莱姆办法,对我来说似乎是一个更有希望的概念。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签