웹 페이지에있는 불완전한 URL에서 완전한 URL을 형성하려면 어떻게해야합니까?
문제
웹 페이지의 텍스트를 검색 할 수 있습니다. https://stackoverflow.com/questions 실제적이고 구성된 링크가 있습니다.
/questions /tags /questions?sort=votes /questions?sort=active randompage.aspx ../coolhomepage.aspx
내 기원 페이지를 아는 것이 었습니다 https://stackoverflow.com/questions .NET에 이에 대한 링크를 해결하는 방법이 있습니까?
https://stackoverflow.com/questions https://stackoverflow.com/tags https://stackoverflow.com/questions?sort=votes https://stackoverflow.com/questions?sort=active https://stackoverflow.com/questions/randompage.aspx https://stackoverflow.com/coolhomepage.aspx
브라우저가 링크를 해결하기에 충분히 똑똑한 방식과 비슷합니다.
=========================== David의 솔루션 사용 : 업데이트 :
'Regex to match all <a ... /a> links Dim myRegEx As New Regex("\<\s*a (?# Find opening <a tag) " & _ ".+?href\s*=\s*['""] (?# Then all to href=' or "" ) " & _ "(?<href>.*?)['""] (?# Then all to the next ' or "" ) " & _ ".*?\> (?# Then all to > ) " & _ "(?<name>.*?)\<\s*/a\s*\> (?# Then all to </a> ) ", _ RegexOptions.IgnoreCase Or _ RegexOptions.IgnorePatternWhitespace Or _ RegexOptions.Multiline) 'MatchCollection to hold all the links that are matched Dim myMatchCollection As MatchCollection myMatchCollection = myRegEx.Matches(Me._RawPageText) 'Loop through all matches and evaluate the value of the href attribute. For i As Integer = 0 To myMatchCollection.Count - 1 Dim thisLink As String = "" thisLink = myMatchCollection(i).Groups("href").Value() 'This checks for Javascript and Mailto links. 'This is not complete. There are others to check I just haven't encountered them yet. If thisLink.ToLower.StartsWith("javascript") Then thisLink = "JAVASCRIPT: " & thisLink ElseIf thisLink.ToLower.StartsWith("mailto") Then thisLink = "MAILTO: " & thisLink Else Dim baseUri As New Uri(Me.URL) If Not thisLink.ToLower.StartsWith("http") Then 'This is a partial URL so we will assume that it's relative to our originating URL Dim myUri As New Uri(baseUri, thisLink) thisLink = "RELATIVE LOCAL LINK: RESOLVED: " & myUri.ToString() & " ORIGINAL: " & thisLink Else 'The link starts with HTTP, determine if part of base host or is outside host. Dim ThisUri As New Uri(thisLink) If ThisUri.Host.ToLower = baseUri.Host.ToLower Then thisLink = "INSIDE COMPLETE LINK: " & thisLink Else thisLink = "OUTSIDE LINK: " & thisLink End If End If End If 'I'm storing the found links into a Generic.List(Of String) 'This link has descriptive text added to it. 'TODO: Make collection to hold only unique internal links. Me._Links.Add(thisLink) Next
해결책
이렇게 말해?
Uri baseUri = new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri, "catalog/shownew.htm");
Console.WriteLine(myUri.ToString());
샘플이 나옵니다 http://msdn.microsoft.com/en-us/library/9hst1w91.aspx
다른 팁
서버 측을 의미하는 경우 사용할 수 있습니다 ResolveUrl()
:
string url = ResolveUrl("~/questions");
이 맥락에서 "Resolve"가 의미하는 바를 이해하지 못하지만 기본 HTML 요소를 삽입 할 수 있습니다. 브라우저가 어떻게 처리 할 것인지 물었습니다.
"그만큼 <base>
태그는 페이지의 모든 링크에 대한 기본 주소 또는 기본 대상을 지정합니다. "
제휴하지 않습니다 StackOverflow