0

I have set up a URL redirect on http://freedns.afraid.org/ so I can change host to a file that my application downloads, without the need of changing the code in my app.

The problem is that I need to resolve the correct URL before downloading the file. I tried a method I found here at SO but it didn't work (Webrequest).

So I suppose that they don't use a common redirect.

How can you resolve the real url/ip?

UPDATE:

I have another subdomain over at freedns, and if you do a downloadstring on that one you get the page that it should redirect to. Maybe that info can be to any help.

UPDATE2:

Here is the code I use to fetch the other webpage:

        WebClient client = new WebClient();
        string xml = client.DownloadString(new Uri("myfreednshere"));

So by running that code, I get the string of the webpage b, that the "myfreednshere" redirects to.

Which means that the webclient succeeds in resolving the url redirect. Is there any code that I can use that just resolves the redirect?

UPDATE3:

This is the response I get with a httprequest:

{X-Abuse: URL redirection provided by freedns.afraid.org - please report any misuse of this service
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Cache-Control: public, max-age=15
Content-Type: text/html
Date: Wed, 09 Nov 2011 21:55:21 GMT
Server: Apache/1.3.41 (Unix) PHP/5.3.6 with Suhosin-Patch
X-Powered-By: PHP/5.3.6

}
fgblomqvist
  • 2,244
  • 2
  • 32
  • 44
  • If your code works for one url and not the other, I would suggest that the problem is in the redirect rather than in your code. If you go to that url in a browser, does it redirect you to the right place? Can you show us the code you're using to fetch the document? – Jim Mischel Nov 09 '11 at 21:44
  • based on toxicious's comment to my answer http://stackoverflow.com/questions/8072052/resolve-url-redirect/8072355#8072355 this should be a question on Freedns? Move to webapps? – McKay Nov 09 '11 at 22:11

3 Answers3

2

I've noticed that at least one afraid.org site (http://fb.afraid.org, the only domain I could get to work with a quick check) does not use HTTP redirection, 301 redirects, or proxying. It uses frames. So, your original code should work:

    WebClient client = new WebClient();
    string xml = client.DownloadString(new Uri("myfreednshere"));

With a little modification, I used this code:

    WebClient client = new WebClient();
    string html = client.DownloadString(new Uri("http://fb.afraid.org"));

the result of the call had the real url (http://www.fragbite.com) in three places, once in a comment, once in a frame source, and once in a link in the noframes tag. You should be able to parse the url out if you need it programatically.

McKay
  • 12,334
  • 7
  • 53
  • 76
  • Have I told you that you're a genius? Well I know I haven't but you truly are one ;) I did a check with firebug and found this tag at the top: So yeah, will just do a scan of that page and get that string. Thank you a lot! – fgblomqvist Nov 10 '11 at 19:16
1

the WebClient class follows redirects. Try using HttpWebRequest:

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.AllowAutoRedirect = false;

After the request is made, one of the HTTP headers "Location", gives the location where it was redirected to (but didn't follow because AllowAutoRedirect was off)

McKay
  • 12,334
  • 7
  • 53
  • 76
  • It gives the code I have written in my answer. "URL redirection provided by freedns.afraid.org - please report any misuse of this service" – fgblomqvist Nov 09 '11 at 22:04
  • If that's the case, then you don't have any control over the redirect. freedns is fetching the data, and getting it for you. You can't control it in code, the proxy is doing that for you. – McKay Nov 09 '11 at 22:09
  • 1
    If you use the above code, you can get the Location (http://en.wikipedia.org/wiki/HTTP_location), which is the url that it's redirecting you to. To get the location, `response.GetResponseHeader("Location")`. It might also be available with `WebClient` through the `ResponseHeaders["Location"]` property. – Jim Mischel Nov 10 '11 at 00:28
  • @JimMischel It didn't work. The header was empty. Well I guess I'll have to do it the non-perfect way and hardcode the url then. Anyway thank you for trying to help me! – fgblomqvist Nov 10 '11 at 10:58
  • @toxicious, like I mentioned in my previous content. That is how you check a redirect. In this case, you are not actually being redirected, you are using a proxy. The server is doing the work for you, instead of telling you where you can go. http://www.wikipedia.com uses a redirect. http://fb.afraid.org is a proxy. When you point your web browser to either, you get content at a different URL, the difference is whether your browser goes to get it (redirect) or the server goes to get it (proxy). If it's a redirect, you can easily get the new URL; if it's a proxy, you cannot. – McKay Nov 10 '11 at 15:37
  • Oh, it looks like afraid.org isn't using a proxy, they are using frames. If you pull the content of the page, you can parse, looking for a frame that points to the right place. – McKay Nov 10 '11 at 15:44
0

So you're wanting a 301 Redirect?

It can be handled a couple of ways. If this is .NET, and if you're using IIS 7, you can use the URL Rewrite module (http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/), or you may be able to modify the web.config file directly if you know what you're doing.

    <system.webServer>
  <httpRedirect enabled="true" destination="http://domain.com/products" httpResponseStatus="Permanent" />
</system.webServer>

Follow this link for more info on how to handle 301's http://knowledge.freshpromo.ca/seo-tools/301-redirect.php

gtr1971
  • 2,672
  • 2
  • 18
  • 23
  • no I don't have the control of the redirect. url a redirects to url b. By doing something with url a I want to retrieve url b. – fgblomqvist Nov 09 '11 at 21:49