asihttprequests失败

时间:2011-03-01 15:20:00

标签: iphone cocoa asihttprequest

我正在尝试使用ASIHTTPRequest下载文件队列。每个请求都会永久失败。我知道网址是有效的,我已将它们追踪出来并将它们弹出到浏览器中,文件就在那里。我对如何调试这个问题的想法不多。

-(void) getRemoteFiles:(NSMutableArray *) M
{
    [self createFileToAppDirectory];

    if (!networkqueue) {
        networkqueue:[[[ASINetworkQueue alloc] init] autorelease];
    }

    [[self networkQueue] cancelAllOperations];

    [self setNetworkQueue:[ASINetworkQueue queue]];
    [[self networkQueue] setDelegate:self];
    [[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
    [[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
    [[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];


    int i;

    for (i=0; i<[M count]; i++) {
        NSString *url=[M objectAtIndex:i];
        NSString* theFileName = [url lastPathComponent];
        if ([theFileName isEqualToString:@"nothing"]==NO) {

            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
            //[request setDownloadDestinationPath:[self getDirectoryPathForFileName:theFileName]];
            [[self networkQueue] addOperation:request];

        }

    }

    [[self networkQueue] go];


}

修改

我想我发现了问题,但我不知道如何修复它。如果我将请求网址更改为http://mysite/content/track.mp3之类的简单内容,则一切正常。但是当我使用原始URL时,请求失败。原始网址看起来像这样

http://site-media.s3.amazonaws.com/2020愿景 - Deep Tech House和Techno / P_122_DrumLp6.mp3

我认为它可能与空间有关,但我认为nsurl会处理那些东西?

1 个答案:

答案 0 :(得分:4)

事实证明,NSURL不会为您安全地编码url字符串。在我的情况下,url中的空格导致问题。洗脱如下

NSString *safestring=[unsafestring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];