2
我有一個URL在到達着陸頁之前重定向到若干個URL。我發現解決方案得到最終的重定向here但我想要所有的中間URL。在使用HttpWebRequest的最終/着陸URL之前捕獲所有中間Url
我有一個URL在到達着陸頁之前重定向到若干個URL。我發現解決方案得到最終的重定向here但我想要所有的中間URL。在使用HttpWebRequest的最終/着陸URL之前捕獲所有中間Url
這工作:
'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。
這很好用..感謝@Neolisk .. – bharat1010 2013-03-14 08:41:46