0

I want to download a sound file from a short URL (like: 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 directly, 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 a short URL (adjix.com/3na3), the 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";
4

3 回答 3

0

NSURLConnection 有一个委托方法,你可以专门实现它来处理重定向。但是,由于您的请求的用户代理字符串,您要连接的服务器的行为可能有所不同。

于 2009-02-11T13:48:52.477 回答
0

您可能想尝试 GTMHTTPFetcher 类,它是Google Toolbox for Mac的一部分。它为您处理重定向。

于 2009-02-08T00:42:14.530 回答
0

你能提供更多信息吗?特别是,最好提及您用于执行 HTTP 请求的类,以及使用重定向 URL 时发生的中断。

于 2009-02-07T19:00:05.890 回答