我的数据库中有一个表,其中包含一些网站的URL.我必须打开这些URL并验证这些页面上的一些链接.问题是某些URL被重定向到其他URL.我的逻辑是没有这样的URL.
有什么方法可以通过我的原始URL字符串并重新获取重定向的URL?
示例:我正在尝试使用此URL:http: //individual.troweprice.com/public/Retail/xStaticFiles/FormsAndLiterature/CollegeSavings/trp529Disclosure.pdf
它被重定向到这个:http: //individual.troweprice.com/staticFiles/Retail/Shared/PDFs/trp529Disclosure.pdf
我试着使用以下代码:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Uris);
req.Proxy = proxy;
req.Method = "HEAD";
req.AllowAutoRedirect = false;
HttpWebResponse myResp = (HttpWebResponse)req.GetResponse();
if (myResp.StatusCode == HttpStatusCode.Redirect)
{
MessageBox.Show("redirected to:" + myResp.GetResponseHeader("Location"));
}
Run Code Online (Sandbox Code Playgroud)
当我执行上面的代码时,它给了我HttpStatusCodeOk.我很惊讶它为什么不考虑重定向.如果我在Internet Explorer中打开该链接,它将重定向到另一个URL并打开PDF文件.
有人可以帮助我理解为什么它不能正常运行示例URL吗?
顺便说一句,我检查了Hotmail的URL(http://www.hotmail.com)并正确返回重定向的URL.
谢谢,