获取301 URl重定向到的位置

时间:2012-02-03 11:04:47

标签: c# url redirect

我有以下几点:

HttpWebRequest webRequest = HttpWebRequest)WebRequest.Create("http://www.google.com/");
webRequest.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
HttpStatusCode wRespStatusCode = new HttpStatusCode();

try
{
    response = (HttpWebResponse)webRequest.GetResponse();
    wRespStatusCode = response.StatusCode;
}
catch (WebException we)
{
    wRespStatusCode = ((HttpWebResponse)we.Response).StatusCode;
}

MessageBox.Show(wRespStatusCode.ToString());

获取HTTP请求的状态代码。

如果是301“永久移动”响应,我想知道如何找到请求被重定向到的新URL?

1 个答案:

答案 0 :(得分:3)

301 status code section in the HTTP specification所示,请查看Location标题,可从HttpWebResponse.Headers媒体资源中访问。