我想检查特定页面是否被重定向。但是,每当我尝试此操作时,我返回的 header 似乎来自重定向页面,而不是最初请求的页面(特别是状态正常,而不是我想看到的 302)。
我可以设置什么使其不会自动遵循重定向吗?
WebRequest request = WebRequest.Create(@"http://www.example.com/page.html");
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.Redirect)
{
...
}
else
{
MessageBox.Show("HTTP Code: " + response.StatusCode + "\r\n\r\n" + response.StatusDescription);
return false;
}
最佳答案
HttpWebRequest.AllowAutoRedirect
Gets or sets a value that indicates whether the request should follow redirection responses.
...
If AllowAutoRedirect is set to false, all responses with an HTTP status code from 300 to 399 is returned to the application.
关于c# - 无需重定向即可获取 HttpWebResponse header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1059888/