If I have a URL like www.mydomain.co.uk/shopping
Is it pos开发者_StackOverflow社区sible to find the real URL of shopping?
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("www.mydomain.co.uk/shopping");
I quickwatched 'req' but couldn't see it?
anyone got any ideas?
If you want to obtain the response anyway, and to also know the redirected-to URI, then use HttpWebRequest
as normal, and examine the ResponseUri
property of the HttpWebResponse
.
If you want to obtain the redirected-to-URI without following the redirect, then set the AllowAutoRedirect
of the HttpWebRequest
to false, and examine the "Location" header of the HttpWebResponse
.
In the later case, it's always possible that there is a subsequent redirect. If you need to know this, then you're probably better of going back to the first method (which will follow the chain of redirects). If you need to know all the redirects you will have to follow the chain yourself. Be careful to limit the total number you will follow to avoid falling into a redirect blackhole (catching duplicates is also a good idea, but you still need an upper limit as blackholes can exist without duplication).
I am assuming you are talking about server URL rewriters, where a URL like this:
http://example.com/product/view/2
Is in fact handled by:
http://example.com/product.aspx?id=2
If the server is doing internal re-routing (such as with MVC routes in IIS), then no, this is not possible, as it is fully internal and the actual page/handler URL does not get exposed to the client.
try this -
Uri ourUri = new Uri(url);
WebRequest myWebRequest = WebRequest.Create(url);
WebResponse myWebResponse = myWebRequest.GetResponse();
string actualUri = myWebResponse.ResponseUri;
精彩评论