bandejapaisa
bandejapaisa

Reputation: 26972

206 Content-Range Response on NSSessionDownloadTask

I'm using a NSSessionDownloadTask to download a large mp4. I've used NSURLSessions lots of times and had no issues, but this time something has me stumped...

I start the request like so:

NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:url];
    self.sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    self.sessionConfig.timeoutIntervalForRequest = 60;
    self.urlSession = [NSURLSession sessionWithConfiguration:self.sessionConfig delegate:self delegateQueue:nil];
    self.downloadTask = [self.urlSession downloadTaskWithRequest:downloadRequest];
    [self.downloadTask resume];

It first calls:

- (void) URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:

and the response headers I get are:

{ status code: 206, headers {
"Accept-Ranges" = bytes;
"Cache-Control" = "max-age=604800";
Connection = "keep-alive";
"Content-Length" = 1001;
"Content-Range" = "bytes 0-1000/566368944";
"Content-Type" = "video/mp4";
Date = "Wed, 29 Jul 2015 11:10:41 GMT";
Etag = "\"21c21ab0-4f46826b4c580\"";
Expires = "Wed, 05 Aug 2015 11:10:41 GMT";
"Last-Modified" = "Wed, 12 Mar 2014 12:41:10 GMT";
Server = Apache;
}

It then immediately calls:

- (void) URLSession:downloadTask:didFinishDownloadingToURL:

..and the request ends.!

What am I missing? Thanks

Upvotes: 2

Views: 1767

Answers (2)

Ramesh_T
Ramesh_T

Reputation: 1261

This is not cache issue. Your server implemented the Range header. This will help you that how much data sent in last request. This request will send the data continuously till it reaches to content-Length this the complete size of your file. You need to send the Range header in you request and handle the response accordingly.

Ex.

    "Accept-Ranges" = bytes;
    "Cache-Control" = "max-age=71726";
    Connection = "keep-alive";
    "Content-Length" = 34538928;
    "Content-Type" = "text/plain";
    Date = "Mon, 04 Jan 2016 10:23:10 GMT";
    Etag = "\"2681502-20f05b0-51966ce4c0380\"";
    Expires = "Tue, 05 Jan 2016 06:18:36 GMT";
    "Last-Modified" = "Fri, 26 Jun 2015 07:37:02 GMT";
    Server = Apache;

I requested the video URL first I received the above header. It started downloading after few seconds I stop the process and restart the app the I got the below headers information

    "Accept-Ranges" = bytes;
    "Cache-Control" = "max-age=71726";
    Connection = "keep-alive";
    "Content-Length" = 34536357;
    "Content-Range" = "bytes 2571-34538927/34538928";
    "Content-Type" = "text/plain";
    Date = "Mon, 04 Jan 2016 10:23:58 GMT";
    Etag = "\"2681502-20f05b0-51966ce4c0380\"";
    Expires = "Tue, 05 Jan 2016 06:19:24 GMT";
    "Last-Modified" = "Fri, 26 Jun 2015 07:37:02 GMT";
    Server = Apache;

If you observe both headers you will find in second header "Content-Range" This means your file is downloaded "bytes 2571" bytes of 34538927 size. Then it will send another packet something like "23672" bytes as per your network speed.

This "Range" and "Content-range" will help you to download the large files in resume manner.

Ex. Suppose 100MB file is there you downloaded 90MB data after this network connection lost. Then you need download the file from start.

To avoid this with the help of "Range" and "Content-Length" you can save the users data.

Hope you will understand

Upvotes: 3

bandejapaisa
bandejapaisa

Reputation: 26972

It appears to be a cache issue.

Prior to this request, I request a license and make a request to the mp4 using the the Range request of 0-1000. So when I request the same url (even without the Range header) it is returning the previous response.

Upvotes: 0

Related Questions