tags:

views:

243

answers:

3

Hi all,
I want do download sound file from short url ( like: http://www.adjix.com ) When i try from normal link it's OK, but from short url, how first redirect and then download

I use this part of code to create request:

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlToSound]];

NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if (theConnection) {
    self.receiveData = [[NSMutableData data] retain];
}

And this code to view header:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [receiveData setLength:0];

    if ([response isKindOfClass:[NSHTTPURLResponse self]]  ) {
     NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
     NSLog(@"headers: %@", headers);
   }
}

When i try to download direct link to mp3 header is:

    "Accept-Ranges" = bytes;
    Connection = "Keep-Alive";
    "Content-Length" = 21316;
    "Content-Type" = "audio/mpeg";
    Date = "Sat, 07 Feb 2009 16:01:34 GMT";
    Etag = "\"2d810-5344-7dda240\"";
    "Keep-Alive" = "timeout=15, max=100";
    "Last-Modified" = "Sat, 25 Jun 2005 12:26:41 GMT";
    Server = Apache;

When i try to download file with short url ( http://adjix.com/3na3 ), header is:

    "Cache-Control" = "max-age=60";
    Connection = close;
    "Content-Length" = 692;
    "Content-Type" = "text/html";
    Date = "Sat, 07 Feb 2009 19:18:23 GMT";
    Expires = "Sat, 07 Feb 2009 19:19:23 GMT";
    Server = "Apache/1.3.41 (Darwin) mod_ssl/2.8.31 OpenSSL/0.9.7l";
A: 

Can you provide more information? In particular, it would be good to mention the classes you're using to perform the HTTP request, as well as what breaks when you use the redirected URL.

Neil Mix
A: 

You may want to try the GTMHTTPFetcher class, which is part of Google Toolbox for Mac. It handles redirection for you.

Chris Lundie
A: 

NSURLConnection has a delegate method which you can implement specifically to handle redirects. However, it may be that the server you are connecting to is behaving differently because of your request's user agent string.

Mike Abdullah