Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 350301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:32:16+00:00 2026-05-12T11:32:16+00:00

Given a URL A which gets redirected to a 3rd party website B, in

  • 0

Given a URL A which gets redirected to a 3rd party website B, in my application I need to to find out URL B for the given url A and insert it in DB , this can be a windows application or web or whichever way is faster and easier using C#!
Thanks !

P.S. I do not require the code to insert in DB.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:32:17+00:00Added an answer on May 12, 2026 at 11:32 am

    WebRequest follows redirects without user intervention, so if the redirects are using 301/302 status codes then the following will work

    WebRequest request = WebRequest.Create(destination);
    WebResponse response = request.GetResponse();
    Console.WriteLine(response.ResponseUri);
    

    If the redirects are created using javascript or HTTP-Equiv meta tags then you’re doing to have to parse the page and look for those. The HTML agility pack is probably the best way to do this.

    To take this a little further the following is a class which will manually resolve the main HTTP redirect status codes, building up a history as it goes

    /// <summary>
    /// Digs through HTTP redirects until a non-redirected URL is found.
    /// </summary>
    public class Digger
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Digger"/> class.
        /// </summary>
        public Digger() : this(20)
        {            
        }
    
        /// <summary>
        /// Initializes a new instance of the <see cref="Digger"/> class.
        /// </summary>
        /// <param name="maximumDepth">The maximum depth of redirects to parse.</param>
        public Digger(int maximumDepth)
        {
            this.MaximumDepth = maximumDepth;
        }
    
        /// <summary>
        /// Gets the maximum depth of redirects to parse.
        /// </summary>
        /// <value>The maximum depth of redirects to parse.</value>
        public int MaximumDepth
        {
            get; 
            private set;
        }
    
        /// <summary>
        /// Resolves any redirects at the specified URI.
        /// </summary>
        /// <param name="destination">The initial URI.</param>
        /// <returns>The URI after resolving any HTTP redirects.</returns>
        public Uri Resolve(Uri destination)
        {
            List<Uri> redirectHistory = new List<Uri>();
            return this.Resolve(destination, redirectHistory);
        }
    
        /// <summary>
        /// Resolves any redirects at the specified URI.
        /// </summary>
        /// <param name="destination">The initial URI.</param>
        /// <param name="redirectHistory">A collection of <see cref="Uri"/> objects representing the redirect history.</param>
        /// <returns>The URI after resolving any HTTP redirects.</returns>
        public Uri Resolve(Uri destination, ICollection<Uri> redirectHistory)
        {
            redirectHistory.Add(destination);
            return this.Resolve(destination, this.MaximumDepth, redirectHistory);
        }
    
        /// <summary>
        /// Resolves any redirects at the specified URI.
        /// </summary>
        /// <param name="destination">The initial URI.</param>
        /// <param name="hopsLeft">The maximum number of redirects left to follow.</param>
        /// <param name="redirectHistory">A collection of <see cref="Uri"/> objects representing the redirect history.</param>
        /// <returns>The URI after resolving any HTTP redirects.</returns>
        private Uri Resolve(Uri destination, int hopsLeft, ICollection<Uri> redirectHistory)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destination);
            request.AllowAutoRedirect = false;
            request.Method = "HEAD";
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
            Uri resolvedUri;
    
            if (response.StatusCode == HttpStatusCode.Redirect || 
                response.StatusCode == HttpStatusCode.Moved || 
                response.StatusCode == HttpStatusCode.MovedPermanently)
            {
                if (hopsLeft > 0)
                {
                    Uri redirectUri = new Uri(response.GetResponseHeader("Location"));
                    if (redirectHistory.Contains(redirectUri))
                    {
                        throw new Exception("Recursive redirection found");
                    }
    
                    redirectHistory.Add(redirectUri);
                    resolvedUri = this.Resolve(redirectUri, hopsLeft - 1, redirectHistory);
                }
                else
                {
                    throw new Exception("Maximum redirect depth reached");
                }
            }
            else
            {
                resolvedUri = response.ResponseUri;
            }
    
            return resolvedUri;            
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 218k
  • Answers 219k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The redirect happens because the user is not authorized to… May 12, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer I got my answer from the YUI Library forum which… May 12, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer Source filters are bad problematic because they use an imperfect… May 12, 2026 at 11:40 pm

Related Questions

I've got a DB that refers to the following url: http://en.wikipedia.org/wiki/Herbert_Gr%F6nemeyer However, it seems
We are replacing an old classic asp website with a .NET 3.5 solution. We
I am working on a URL shortening site which uses PHP, MySQL and Apache.
I have a getJSON call which is inexplicably failing. The idea is, you click

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.