如何获得重定向响应

假设我将www.abc.com放入浏览器,浏览器会自动重定向到www.xyz.com。 我需要从服务器端获取该重定向URL。 也就是说,如果www.abc.com返回重定向urlwww.xyz.com,我该如何从原始url(www.abc.com)请求此重定向url(www.xyz.com)?

以下是网络抓取工具的一个片段,展示了如何处理重定向:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.AllowAutoRedirect = false; // IMPORTANT webRequest.UserAgent = ...; webRequest.Timeout = 10000; // timeout 10s // Get the response ... using (webResponse = (HttpWebResponse)webRequest.GetResponse()) { // Now look to see if it's a redirect if ((int)webResponse.StatusCode >= 300 && (int)webResponse.StatusCode <= 399) { string uriString = webResponse.Headers["Location"]; Console.WriteLine("Redirect to " + uriString ?? "NULL"); ... } }