我有一个URL,在到达目标网页之前会重定向到多个网址。我找到了获得最终重定向here的解决方案,但我想要所有中间URL。
答案 0 :(得分:1)
这有效:
'http://tinyurl.com/gtrp -> points to google.ca
Dim urls As List(Of String) = GetAllRedirectURLs("http://tinyurl.com/gtrp")
'urls(0) = "http://google.ca"
'urls(1) = "http://www.google.ca/"
Private Function GetAllRedirectURLs(path As String) As List(Of String)
Dim urls As New List(Of String)
Dim request As HttpWebRequest = WebRequest.Create(path)
request.Method = "HEAD"
request.AllowAutoRedirect = False
Dim response As HttpWebResponse = request.GetResponse
If response.StatusCode = HttpStatusCode.Moved Or response.StatusCode = HttpStatusCode.Redirect Then
Dim redirectsToURL As String = response.GetResponseHeader("Location")
urls.Add(redirectsToURL)
urls.AddRange(GetAllRedirectURLs(redirectsToURL))
End If
Return urls
End Function
使用递归来深入挖掘和收集链接。基于this question。