Detecting a final Url after redirect

2019-08-29 18:04发布

问题:

I tried to detect the final Url after a redirect. The Url is shown below. When using chrome I receive the 302 response however in my code I simply see a 200 without an Url change. I'm able to see 302 status using chrome dev tools.

http://imgur.com/ZmQOSHo (Chrome console image at this link)

I've tested with another Url which works. I've noticed the latency for the tiny url is 1.21 s and for the other Url is 85 ms. Maybe the Redirect is so fast it doesn't get noticed?

I've included my code at the bottom.

References used trying to detect the redirect:

Getting the Redirected URL from the Original URL

Get a collection of redirected URLs from HttpWebResponse

Works: http://tinyurl.com/qcrr3kn

Not working: 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