我尝试使用此简单代码获取重定向代码307,但失败了。
string urlRequest = "http://www.google.com";
request = HttpWebRequest.Create(urlRequest) as HttpWebRequest;
request.AllowAutoRedirect = false;
var response = request.GetResponse();
期望响应状态代码为307,AbsoluteUri =“ https://www.google.com”,但不是吗?
答案 0 :(得分:2)
在这种情况下,Google无法启动重定向,因为它无法确定客户端是否支持https。似乎Google检查了请求的UserAgent标头,并且仅在可以确定用户代理支持https时才启动重定向。
string urlRequest = "http://www.google.com";
HttpWebRequest request = HttpWebRequest.CreateHttp(urlRequest);
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0";
request.AllowAutoRedirect = false;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
MessageBox.Show(response.StatusCode.ToString());
MessageBox.Show(response.Headers["Location"]);
其他请求标头也会影响Google的行为。