c# - 无需重定向即可获取 HttpWebResponse header

标签 c# http redirect header httpwebresponse

我想检查特定页面是否被重定向。但是,每当我尝试此操作时,我返回的 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/

相关文章:

c# - 解析以 T24 :00:00Z 结尾的 JSON 日期

c# - 无法使用 DI 选项在 AspNetCore 2.0 应用程序中创建 DbContext 的实例

c# - Net Core 集成测试 : Run Startup. cs 和来自其他程序的配置

http - Mule 中的接收器线程配置文件 HTTP 配置

c# - 正则表达式拆分有异常(exception)

http - 使用 RestSharp 和 ResponseWriter 下载流

c++ - 尝试发送 GET 请求,同一地址收到 301

php - .htaccess 重定向匹配。匹配除一个单词之外的字符串

mod-rewrite - 如何在 JBoss 服务器上重写 URL?

linux - 重定向后的参数有什么作用吗?