版权保护声明:本文未经许可,禁止任何形式转载,违者必究。
在实际开发过程中,往往我们需要从一个已知的URL跟踪其在网络中的重定向的地址列表,参考了查了一下资料,Stackoverflow上的解决方案:http://stackoverflow.com/questions/17990604/get-a-collection-of-redirected-urls-from-httpwebresponse
public static string RedirectPath(string url) { StringBuilder sb = new StringBuilder(); string location = string.Copy(url); while (!string.IsNullOrWhiteSpace(location)) { sb.AppendLine(location); // you can also use 'Append' HttpWebRequest request = HttpWebRequest.CreateHttp(location); request.AllowAutoRedirect = false; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { location = response.GetResponseHeader("Location"); } } return sb.ToString(); }
版权保护声明:本文未经许可,禁止任何形式转载,违者必究。
发表评论
登录用户才能发表评论, 请 登 录 或者 注册