我尝试在重定向后检测最终的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
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