本文介绍了我要如何检查一个302回应? WebRequest的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的WebRequest我想知道如果我得到一个302暂时移动的回应,而不是自动获取新的网址

Using WebRequest i want to know if i get a "302 Moved Temporarily" response instead of automatically get the new url

推荐答案

如果要检测,而不是跟随它自动创建的WebRequest ,并设置一个重定向响应, AllowAutoRedirect 属性

If you want to detect a redirect response, instead of following it automatically create the WebRequest and set the AllowAutoRedirect property to false:

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

这篇关于我要如何检查一个302回应? WebRequest的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 15:54