How to get the response (which is a redirect instruction) of a request from server side? For example if I put www.abc.com in the browser the browser automatically gets redirected to www.xyz.com. Now I need to get that redirect url from server side. i.e. need send a request to www.abc.com in response it returns a redirect url www.xyz.com, need to store this xyz.com. Any Idea how this can be done?
+1
A:
Here's a snippet from a web crawler that shows how to handle redirects:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.AllowAutoRedirect = false; // IMPORTANT
webRequest.UserAgent = ...;
webRequest.Timeout = 10000; // timeout 10s
// Get the response ...
webResponse = (HttpWebResponse)webRequest.GetResponse();
// Now look to see if it's a redirect
if ((int)webResponse.StatusCode >= 300 && (int)webResponse.StatusCode <= 399)
{
string uriString = webResponse.Headers["Location"];
Console.WriteLine("Redirect to " + uriString ?? "NULL");
webResponse.Close(); // don't forget to close it - or bad things happen!
...
Hightechrider
2010-10-01 05:43:44
Worked perfectly. thnx.
Rahat
2010-10-01 16:43:02