使用NSURLConnection时连接丢失错误

时间:2013-03-21 06:07:53

标签: iphone nsurlconnection

使用NSURLConnection时,我遇到连接丢失问题。我正在使用NSURLConnection进行异步下载。我正在下载大小约80MB的大文件。我每次都使用适当的文件处理将接收的数据写入文件中。一段时间后,我在NSURLConnection委托的方法名为didFailWithError的方法中遇到连接错误“Connection Lost”。如果我在Mac上的模拟器中执行,则需要很长时间,但文件会成功下载而不会出现连接丢失错误。有什么建议如何避免这个错误?或者这个错误背后的原因是什么?

如果需要任何细节,请告诉我。请注意,我已经阅读了类似的帖子,但它没有帮助我。

1 个答案:

答案 0 :(得分:-1)

查找以下代码段,如果需要更多信息,请与我们联系:

-(void) startDownloadFromURL:(NSString*)URLString
{
    if(URLString == nil)
    {
        [delegate DownloadFailed:-1];
        return;
    }
    //self.pstrBaseFilePath = filePath;
    URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

    NSMutableURLRequest* pRequest = [[NSMutableURLRequest alloc] init];
    [pRequest setURL:[NSURL URLWithString:URLString]];

    if(gpUserDataManager.pstrSessionID == nil)
        return;

    [pRequest addValue:[@"ASessionID=" stringByAppendingString:gpUserDataManager.pstrSessionID]  forHTTPHeaderField:@"Cookie"];

    [pRequest setHTTPMethod:@"GET"];
    [pRequest setTimeoutInterval:180];


    self.urlConnection = [[NSURLConnection alloc] initWithRequest:pRequest delegate:self];

    [urlConnection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    if([httpResponse statusCode] == 200)
    {

    }
    else
    {
        //Start.NOODLE-13304
        /* NSInteger iResponseCode = [httpResponse statusCode];
         NSString* pstrStr = [NSString stringWithFormat:@"%d", iResponseCode];

         //pTheConnection = nil;

         [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Response Error", @"")
         message:pstrStr
         delegate:nil
         cancelButtonTitle:NSLocalizedString(@"OK", @"")
         otherButtonTitles:nil]  show];
         */
        [AUserDataManager ProcessResponseCode:httpResponse.statusCode];
        [self.urlConnection cancel];
        [delegate DownloadFailed:httpResponse.statusCode];
        //End.NOODLE-13304
    }

    //[self.pRecvdata setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if(self.recvdData == nil)
    {
        self.recvdData = [[NSMutableData alloc] init];
    }

    [self.recvdData appendData:data];
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

    //    bIsResponseOK = FALSE;
    //
    //    [NSThread detachNewThreadSelector: @selector(SpinEnd) toTarget:self withObject:nil];
    //
    //    pTheConnection = nil;

    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Connection Error", @"")
                                message:[error localizedDescription]
                               delegate:nil
                      cancelButtonTitle:NSLocalizedString(@"OK", @"")
                      otherButtonTitles:nil]  show];
    [self.urlConnection cancel];
    [delegate DownloadFailed:-1];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [connection cancel];
    [delegate DownloadCompleted:self.recvdData];
}

DownloadRequest *request = [[DownloadRequest alloc] init];
request.delegate = self;
[request startDownloadFromURL:strURL];