C#检测页面重定向 [英] C# detect page redirect
问题描述
我想,以确定是否资质上存在 http://www.accreditedqualifications.org.uk
形式:
I am trying to determine if a qualification exists on http://www.accreditedqualifications.org.uk in the form:
HTTP:// WWW .accreditedqualifications.org.uk /认证/ 50084811.seo.aspx
50084811是一个合格的目标最终用户输入。
50084811 being a qualification aim entered by the end user.
如果他们输入了无效的如
If they enter an invalid one e.g.
的 http://www.accreditedqualifications.org.uk/qualification/50084911.seo.aspx
它们重定向到一个错误页面(不正确的HTTP头,据我可以看到)。有没有一种方法来检测在C#中的重定向。我希望能够检测HTTP头重定向(认为它会发出2)或反对不必下载整个页面相似。这可能是发生了很多,所以我想尽量减少交通。
They are redirected to an error page (with incorrect http headers as far as I can see). Is there a way to detect the redirect in C#. I would hope to be able to detect the redirect in http headers (thinking it will issue 2) or similar as oppose to having to download the whole page. This could be happening a lot so I would like to minimize traffic.
修改
用于这看看头看起来像两个是一个无效页面发出:
Used this to have a look at the headers looks like two are issued for an invalid page:
推荐答案
最简单的方法可能是通过获取HEAD请求(集的 方法
以在头)的HttpWebRequest
已经设置 AllowAutoRedirect
为false。我不记得随便是否会导致异常或没有,但无论哪种方式,它应该很容易处理的。
The simplest way is probably to fetch the content using a HEAD request (set Method
to "HEAD") in an HttpWebRequest
having set AllowAutoRedirect
to false. I can't remember offhand whether that will cause an exception or not, but either way it should be easy to handle.
这篇关于C#检测页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!