2015-08-27 57 views

回答

2

可以使用HttpWebRequest做到這一點:

public static string GetRedirectedUrls(string url) 
{ 
    StringBuilder sb = new StringBuilder(); 
    while (!string.IsNullOrWhiteSpace(url)) 
    { 
     sb.AppendLine(url); 
     HttpWebRequest request = HttpWebRequest.CreateHttp(url); 
     request.AllowAutoRedirect = false; 
     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
     { 
      url = response.GetResponseHeader("Location"); 
     } 
    } 

    return sb.ToString(); 
} 
相關問題