dataTaskWithRequest problem - can't download gzip

Hi everyone,


I have a problem while trying to session.dataTaskWithRequest in a function to read gzip from URL.

The server side changes ".gzip" to ".bin" and stores the file.

I want to read the file with ".bin". However, The network connection was lost.


However, a "The network connection was lost" error will occur.

Could you tell me how to solve this problem?


server side

file name : ***.bin

(This bin file is a gzip file.)


following the code:

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

NSURL *url = [NSURL URLWithString:@"http://...../***.bin"];

NSURLSessionDataTask *task = [session dataTaskWithURL: url
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error) {
            NSLog(@"error [%@]", [error localizedDescription]);

        }
        else {
            NSLog(@"success");

        }
    }];

[task resume];

It’s hard to answer this without knowing more about your server. Many servers support on-the-fly gzip compression via the

Accept-Encoding
/
Content-Encoding
mechanism.
NSURLSession
supports this automatically, that is:
  • NSURLSession
    always lists
    gzip
    in the
    Accept-Encoding
    header on the request.
  • If the server indicates that the content was actually gzipped, via the

    Content-Encoding
    header, then
    NSURLSession
    will decompress the data before return it to you.

So, if you’re trying use this mechanism then there should be nothing for you to do; it should all work automatically behind the scenes.

If, on the other hand, you’re trying to download a resource that’s actually been gzipped but that fact is not reflected in the

Content-Encoding
header,
NSURLSession
will not automatically decompress this for you. You will have to decompress it yourself. For gzip this isn’t too hard, in that there’s support for this in
libz
which is a supported iOS API.

With regards your “network connection was lost” problem, it seems likely that this is a problem with the server itself. QA1941 Handling “The network connection was lost” Errors explains the backstory here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
dataTaskWithRequest problem - can't download gzip
 
 
Q