简体   繁体   中英

How to get redirect url of http://www.google.com site

I have tried this simple code to get redirect code 307 but failed.

string urlRequest = "http://www.google.com";
request = HttpWebRequest.Create(urlRequest) as HttpWebRequest;
request.AllowAutoRedirect = false;
var response = request.GetResponse();

Expect response status code is 307 and AbsoluteUri = " https://www.google.com " but not?

Google does not initiate a redirect in this case because it cannot be sure that the client supports https. It seems that google checks the UserAgent header of the request and only initiates a redirection when it can be sure the user agent supports 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"]);

Other request headers will also influence how Google behaves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM