iOS8: NSURLSession: NSURLSessionDataTask 'Client closed connection before receiving entire response'

2k views Asked by At

I use iOS BACKGROUND FETCH n BACKGROUND TRANSFER to update our app.

When we download an MP4 as part of a large download of lots of files the download can stop suddenly and get error:

'Client closed connection before receiving entire response'

http code is still 200.

We have no limit on data due to corporate cellular accounts and have single apps on our iPad so management want us to download a larger amount of data and number of files sometime over 20/30 on a clean install. They're corporate videos/ pdfs published daily.

I created a NSURLSession

NSURLSession * backgroundSession_ = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier]

Then for each URL to the mp4 or PDF I create a NSURLSessionDownloadTask from my NSURLSession

NSURLSessionDownloadTask * downloadTask_ = [backgroundSession_ downloadTaskWithURL:url_];

The fire off resume on each download task. When all return the NSURLSession completes and I tell the user in a notification that the apps has been updated.

I use CHARLES PROXY on my mac to monitor the downloads. It takes a while as there are many files and also I noticed the movies seem to stream so can be slow. Usually it checks whats been downloaded and only downloads the latest but if I do a lot of downloads say to a clean install I noticed that quiet often the streams to the mp4 get killed. They have a 200 code but with message

'Client closed connection before receiving entire response'

Any idea what may cause this because it can stop my NSURLSession from completing so my sync can get in a sort of hung state.

IMAGE: OK DOWNLOAD: MOVIE STREAMS AND SAVED TO DISK: 200: Complete

IMAGE:OK DOWNLOAD: MOVIE STREAMS AND SAVED TO DISK: 200: Complete

IMAGE: DOWNLOAD SUDDENLY STOPS:

IMAGE: DOWNLOAD SUDDENLY STOPS: IMAGE: DOWNLOAD SUDDENLY STOPS:

Any ideas: I use Background Fetch so not sure if the client in the message is iOS8 or my app. After each downloadtask returns I copy the tmp file to mp4 in documents and call

[session finishTasksAndInvalidate];

if some of these downloadtask fail then I notice the session never finishes and gets stuck. When I do another fetch I get all these old download task and old sessions.

2

There are 2 answers

0
NSKevin On

You can ask your server mate to change the content-type to video/mpeg4 or you can change your request type from GET to POST

0
brian.clear On

was asked how I solved this but two years laster.

We also shrunk the movie files as well to speed up downloads. I also remember the session and the configuration having separate settings. I had set header in one but failed when iOS 8/9 came out as it should have been set in the session not just the config - but may be fixed now as iOS 10 out

//-----------------------------------------------------------------------------------
    //some movie streams time out n get
    //'http://stackoverflow.com/questions/23428793/nsurlsession-how-to-increase-time-out-for-url-requests'
    //'Client closed connection before receiving entire response'

    NSLog(@"urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest:%f", urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest);
    NSLog(@"urlSessionConfigurationBACKGROUND_.timeoutIntervalForResource:%f", urlSessionConfigurationBACKGROUND_.timeoutIntervalForResource);

    //was 60
    //urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest = 240.0;
    urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest = 600.0;
//    urlSessionConfigurationBACKGROUND_.timeoutIntervalForResource = 60.0;