重定向后检测最终的Url

时间:2014-08-29 00:32:56

标签: c# asp.net url httpwebrequest

我尝试在重定向后检测最终的Url。网址如下所示。使用chrome时,我会收到302响应,但在我的代码中,我只看到200没有更改Url。我可以使用chrome dev工具查看302状态。

http://imgur.com/ZmQOSHo(此链接中的Chrome控制台图片)

我已经测试了另一个有效的Url。我注意到这个小网址的延迟是1.21秒,而另一个网址的延迟是85毫秒。也许重定向是如此之快,以至于它没有引起注意?

我已将代码包含在底部。

尝试检测重定向时使用的引用:

Getting the Redirected URL from the Original URL

Get a collection of redirected URLs from HttpWebResponse

作品:http://tinyurl.com/qcrr3kn

不工作:http://api.citygridmedia.com/content/places/v2/click?q=urVBa6moXC5c7fD6SHv-hbf_7HRTJ-epI2pHQq9_3jGRS3oxUEA3zsS1yLhsh9JEMiM8qdYEtS_lStDSZc-z0HmZe5l2XwN1a5djnuXu5V1qx7Z8EcA6yxLKrMog1OH9vFD9461355g648hyYiUbBiYsgkiDMf0VK0OyE5NeCh7xaY-OWE-pI6J9TXcyr1kJ5B5hTzbOJpxvXVdX05ZCdwHpYplILuELic5q1ojeI1nhJzk8Tl7wsl-lPrROPRwrv6Vvvc3wVE2idwR784UoxXgZt00XiBh6rVYpGf0-q80smzQZRNmEcO7wa3342DmMbLPlCUd6QMCCSyvMlmgc4Q7yPuDG5vGRdaSPzm47_mTd74r6ovP00DvY5ODHN3LETLY7LMYirpiUus0hR1WWmUC26M-JCspa2l7J14e2WcsVUHKek47lJyLmPoAqUFoLFNwKZyOqQKk

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);HttpContext.Current.Request.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36";

request.Method = "GET";
request.AllowAutoRedirect = false;

string myTestUri = request.RequestUri.ToString();

string testResponse = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

int statusCode = (int)response.StatusCode;

Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (String.IsNullOrEmpty(response.CharacterSet) == true)
readStream = new StreamReader(receiveStream);
else
readStream = new StreamReader(receiveStream, System.Text.Encoding.GetEncoding(response.CharacterSet));
output = readStream.ReadToEnd();
string newUrl = response.ResponseUri.AbsoluteUri;//  api.citygridmedia.com domain is returned

string uriString = response.GetResponseHeader("Location"); //  empty for the api.citygridmedia.com domain

0 个答案:

没有答案
相关问题