我有一个将我重定向到其他网站的网址。我想从C#中获取原始目标网址。有没有办法遵循这些重定向?
答案 0 :(得分:4)
您可以使用HttpWebRequest类
var request = (HttpWebRequest)WebRequest.Create(someUrl);
request.AllowAutoRedirect = false;
var response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode == HttpStatusCode.Found) // Found == 302
{
// Do something...
string newUrl = response.Headers["Location"];
}
此外,您可以通过设置:
自动关注重定向request.AllowAutoRedirect = true;
request.MaximumAutomaticRedirections = 4; //number of redirections allowed