Gareth Harding Gareth Harding - 1 year ago 85
C# Question

Get where a 301 URl redirects to

I have something along the lines of:

HttpWebRequest webRequest = HttpWebRequest)WebRequest.Create("");
webRequest.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
HttpStatusCode wRespStatusCode = new HttpStatusCode();

response = (HttpWebResponse)webRequest.GetResponse();
wRespStatusCode = response.StatusCode;
catch (WebException we)
wRespStatusCode = ((HttpWebResponse)we.Response).StatusCode;


which gets the status code of of an HTTP request.

In the case of a 301 "Moved Permanently" response, I was wondering how I might find the new URL that the request is being redirected to?

Answer Source

As indicated in the 301 status code section in the HTTP specification, look into the Location header, accessible from the HttpWebResponse.Headers property.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download