2

我有一个 URL,它在到达着陆页之前会重定向到多个 URL。我在这里找到了获得最终重定向的解决方案,但我想要所有中间 URL。

4

1 回答 1

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

使用递归向下钻取和收集链接。基于这个问题

于 2013-03-13T18:55:35.280 回答