如何检测URL是否重定向到另一个URL? [英] How can I detect if a URL is redirected to another one?

查看:550
本文介绍了如何检测URL是否重定向到另一个URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码向网址发出"GET"请求.

I use this code to make a 'GET' request to urls.

var request = WebRequest.Create(uri) as HttpWebRequest;
request.Method = "GET";
var response = request.GetResponse() as HttpWebResponse;
var status = response.StatusCode; // returns HttpStatusCode.OK

因此,当URL重定向到另一个URL时,我无法检测到该问题. 例如;

So, the probelem is when a url is redirected to another one, I can't detect it. For example;

url is: http://site.com

that redirects to http://www.site.com

,但是当我向http://site.com发出请求时,它会为其返回HttpStatusCode.OK-而不是重定向代码.请问有什么想法吗?

but when I make a request to http://site.com it returns a HttpStatusCode.OK for it -instead of a redirect code. Have you any idea please?

推荐答案

您可以使用

You can use the AllowAutoRedirect property. Set it to false and handle the redirect yourself.

这篇关于如何检测URL是否重定向到另一个URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆