views:

30

answers:

2

Possible Duplicate:
A way to figure out redirection URL

C# Issue:

If we type the following url (see warning) http://migre.me/13rQa

It will redirect to oooooo1.ru

I like to know the redirected URL from C#. Anybody knows how to do it.

P.S: These mentioned URL could be virus affected. Please use on own risk

+2  A: 

If you're using HttpWebRequest, set AllowAutoRedirect to false. You'll then get to see the redirect yourself. Note that this only applies to HTTP-level redirects, not meta redirects within HTML.

(I don't know offhand whether it will throw an exception, but even if it does you can examine the response to find out the status code and the data including the redirection.)

Jon Skeet
Actually, it has multiple redirects, so you'd need to follow them through.
Marc Gravell
@Marc: Right. But at least you'd get to see each step of the way, and block it if necessary. It depends what the OP is trying to do.
Jon Skeet
+2  A: 

If migre.me has some kind of an API, maybe they can provide you with a "preview" of the site you'll be redirected to.

If they don't, you'll have to create an HttpWebRequest and see the Uri of the response.

e.g.

WebRequest request = WebRequest.Create("http://migre.me/13rQa");
var response = request.GetResponse();

With that code, the response.ResponseUri property will have the address you'll be redirected.

Hope this helps you

willvv
I think @Jon's answer is better, because you won't have to download the target of the redirect (as you would have to do with my solution). Just use response.Headers["Location"] after setting the AllowAutoRedirect to false as suggested by Jon.
willvv
Actually, since there are multiple redirects involved, yours works and the `AllowAutoRedirect` approach *doesn't*.
Marc Gravell