我如何检查302响应?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (51)

用WebRequest我想知道如果我得到一个

302 Moved Temporarily"

响应而不是自动获取新的url。

提问于
用户回答回答于

如果要检测重定向响应,而不是自动创建WebRequest并将AllowAutoRedirect属性设置为false:

HttpWebRequest request = WebRequest.Create(someUrl) as HttpWebRequest;
request.AllowAutoRedirect = false;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.Redirect || 
    response.StatusCode == HttpStatusCode.MovedPermanently)
{
    // Do something here...
    string newUrl = response.Headers["Location"];
}
用户回答回答于

就像这样:

HttpWebResponse response;
int code = (int) response.StatusCode;

代码应该是

HttpStatusCode.TemporaryRedirect

扫码关注云+社区