Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get 302 redirected url from an existing url in asp.net c#

Posted on 2013-01-07
5
Medium Priority
?
704 Views
Last Modified: 2013-01-24
how can we get the 302 redirected url from an old url in asp.net c# e.g. i have a url below
http://www.scotland.gov.uk/Publications/1997/01/10593
and its redirecting to http://www.scotland.gov.uk/Publications/1997/01/10593/File-1 and I need the new urls from a list of existing urls.
0
Comment
Question by:mmalik15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38750603
You can do the following:

using System.Net;

namespace ConsoleApplication48
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpWebRequest request = HttpWebRequest.Create("http://www.scotland.gov.uk/Publications/1997/01/10593") as HttpWebRequest;
            HttpWebResponse response;
            string redirectedUrl = null;

            request.Method = "GET";
            response = request.GetResponse() as HttpWebResponse;

            if (response.StatusCode == HttpStatusCode.Found)
            {
                redirectedUrl = response.Headers["Location"];
            }
        }
    }
}

Open in new window

0
 

Author Comment

by:mmalik15
ID: 38755582
thanks for the comment kaufmed.

I have slightly altered the code. response.StatusCode is always 'OK' even when there is a redirect. I m comparing the response.ResponseUri with the original URL and its seems to be working though for only initial two requests but starts timing out then. I have a list of 13000 urls to test in a loop and wondering if that is possible. My code as below

 class Program
    {
        static void Main(string[] args)
        {
             string line;
            // Read the file and display it line by line.
            System.IO.StreamReader file =   new System.IO.StreamReader(@"C:\Users\MehtabM\Documents\URIFiles\ScotGov1.txt");

            List<string> href = new List<string>();

            while ((line = file.ReadLine()) != null)
            {
              href.Add(line);
            }

            file.Close();

            HttpWebRequest request; 
            HttpWebResponse response;
            

            foreach(string item in href)
            {
                request=HttpWebRequest.Create(item) as HttpWebRequest;
                request.Method = "GET";
                request.ContentType = "text/xml";
                response = request.GetResponse() as HttpWebResponse;


                if (response.ResponseUri.ToString() != item)
                {
                    item.Replace(item, response.ResponseUri.ToString());
                }
                response = null;

            }

           


        }
    }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38761874
If this is a list of URLs that all point to the same host, then it could be that the server is blocking your connections because you are hitting it with too many requests in a short period of time. You might try inserting a delay between each call.

As to the "response.StatusCode is always 'OK'" I'm not sure. The one I tested in the example returned HttpStatusCode.Found. If your method works, then it should suffice  : )
0
 

Author Comment

by:mmalik15
ID: 38813627
thanks again kaufmed for the help.

If I change the  request.Method = "HEAD" This way we will not load whole page. Server only returns the headers and does not block subsequent requests.
0
 

Author Closing Comment

by:mmalik15
ID: 38813645
Excellent
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question