tags:

views:

161

answers:

2

How can I know where a url gets redirected programatically?

e.g. This url:

http://scobleizer.com/feed/

redirects to the following url:

http://feeds2.feedburner.com/fastcompany/scobleizer

I am trying to read the xml document of feed. But the following lines of code:

var doc = new XmlDocument();
doc.Load(url);

will throw an exeption for the first url:

System.Net.WebException: The remote server returned an error: (404) Not Found.

+1  A: 

That shouldn't throw. It works fine for me:

        var doc = new XmlDocument();
        doc.Load(@"http://scobleizer.com/feed/");

(has now loaded with the feed)

(edit: it was working, but now I'm getting 404.... odd)

If you need to know the final url, you will possibly have to use HttpWebRequest manually.

Marc Gravell
I couldn't manage to read the final url using HttpWebRequest, I have to try more
Maysam
A: 

If it gives you a 404 it's likely because the feed generator checks your user-agent or similar, and then responds 404 when it doesn't meet the requirements.

I agree with Marc: Use a WebRequest and check the response, it should be a 301 or 302 (Permanent or temporary redirect) and it will contain the target url then.

If your response code isn't 301 or 302, then it's not redirecting you - again this could be caused by a user-agent check serverside.

Steffen