How to get a redirection response
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 ...
using (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");
...
}
}
Rahatur
Over the last 13+ years, I have been developing wide range of web applications and eCommerce sites using Asp.net, MVC, Web API, C#, Telerik, SQL Server, HMTL5, JavaScript, Jquery, Bootstrap and Ajax. I have been working for startup companies and small/medium businesses. My core competency lies in complete end-end management of web applications, web services and back-end services for mobile applications. I am seeking opportunities to build web and mobile applications from the ground up for you or your business. I also have experiences in the following areas: Software Architecture, Analysis, Website Security, Web/REST services, mobile application development, OOP, software design and testing. I have been developing desktop, web and mobile applications based on: C#, Asp.net MVC, Web API, Web security, Entity Framework, SignalR etc. eCommerce, Payment gateway, Shopping Cart, nopCommerce framework CSS, xHtml, Html, Psd to Html, DHtml Webservice, Ajax, Json, SOAP, XML Database, MSSQL, MySQL, MS Access Pdf, Excel, CSV JavaScript, Jquery, AngularJs Experience with 3rd party API integration Web Scraper, Data Mining, Business Intelligence, Reporting I consider myself self-taught and always willing to acquire additional necessary skills. I only apply for the jobs which correspond to my skills. I really enjoy working on the chosen projects. Which allows me to continue with a higher rate of client satisfactions and thus excellent ratings. I am available on Skype and email for most part of the day. Do you need to discuss a project? Please feel free to knock me on Skype. I will be glad to assist you on after delivery if you have any question.
Updated on April 22, 2021Comments
-
Rahatur about 3 years
Say if I put www.abc.com in the browser, the browser automatically gets redirected to www.xyz.com. I need to get that redirect url from server side. That is, if www.abc.com returns a redirect url www.xyz.com, how can I request this redirect URL (www.xyz.com) from the original URL (www.abc.com)?
-
Dmitry Fedorkov about 10 yearsFor some reason my edit was not accepted... Calling
Close()
in such way is a bad practice, you should useusing
instead. -
artifex_somnia over 9 yearsthis does not work for me. I'm getting a null value although I'm able to connect to the resource