What are alternatives to NSURLConnection for chunked transfer encoding
Asked Answered
P

3

10

I've checked for other questions relevant to this, but the only answer is "Use ASIHTTPRequest" as this is no longer being developed I wanted to ask what alternatives people are using, whilst working on our SDK I came across a lot of strange behaviour in NSURLConnection when receiving data from the server.

We tracked it down to the fact that NSURLConnection doesn't deal well with responses in chunked-encoding. Or at least so we read in this question here NSURLConnection and "chunked" transfer-coding

Some developers we were talking to say it gets better in iOS 5, we need to make sure that our SDK is backwards compatible with iOS 4.3 at least.

I want to confirm this is infact an issue in NSURLConnection, and how people are dealing with it.

All the alternatives I've found so far are based off of NSURLConnection and I'm assuming as such will have the same flaw. ASIHTTPRequest did in fact work because it's based a little lower than NSURLConnection, but were looking for alternatives in the knowledge it's no longer supported.

A list of other libraries looked at are: Restkit, ShareKit, LRResty, AFNetworking, TTURLRequest

I'm aware there are similar questions here Is RESTKit a good replacement for ASIHTTPRequest? and here ASIHTTPRequest alternative But both of the solutions are based off NSURLConnection.

EDIT: I noticed I pointed to the wrong question at the start of my post, so thats updated. It points to a thread from 2008, and i've seen similar ones but none that are recent.

Pulchia answered 15/2, 2012 at 15:31 Comment(1)
Not yet, I wanted to check that other developers had seen similar results. I just realised that I've put the wrong link in my question. I meant (#8606993). I've updated the post with more info.Pulchia
A
19

Chunked transfers are supported by NSURLConnection. I use them.

  1. Define some props:

    NSMutableData * responseData;
    NSURLConnection * connection;
    
  2. Establish a connection

    NSURL *url = [NSURL URLWithString:@"...."];
    self.responseData = [[NSMutableData alloc] initWithLength:0] ;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    
  3. Register your callback method for connection established

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
         // You may have received an HTTP 200 here, or not...
         [responseData setLength:0];
    }
    
  4. Register your callback method for "chunk received"

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        NSString* aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    
        NSLog(@"This is my first chunk %@", aStr);
    
    }
    
  5. Register your "connection finished" callback:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
       [connection release];
    }
    
  6. And finally, register you "connection failed" callback:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Something went wrong...");
}
Allowance answered 27/2, 2012 at 23:3 Comment(3)
As you can see, It's fair simple and you don't need to use any other framework. I use ASIHTTP every single day, but for chunked transfer encoding, this is enough :)Allowance
i3.kym-cdn.com/photos/images/newsfeed/000/150/505/…Allowance
Thanks, I'll give this a test as soon as I get the time to dig into the SDK again and I'll make sure to set this as the answer if it works.Pulchia
C
9

Just to chime in for the next person that gets here and still can't get NSURLConnection to work with chunk encoded data.

NSURLConnection will work with chunked encoding, but has non-disclosed internal behaviour such that it will buffer first 512 bytes before it opens the connection and let anything through IF Content-Type in the response header is "text/html", or "application/octet-stream". This pertains to iOS7 at least.

However it doesn't buffer the response if Content-Type is set to "text/json". So, whoever can't get chunked encoded NSURLConnection responses to work (i.e. callbacks aren't called) should check the response header and change it on the server to "text/json" if it doesn't break application behaviour in some other way.

Catheterize answered 12/6, 2014 at 10:36 Comment(3)
I don't understand the logic in the second paragraph " it doesn't buffer the response if Content-Type is set to "text/json" and "and change it on the server to "text/json" appear to be contradictory. Don't you want NSURLConnection to buffer the response? If not why not?Aalesund
I'm not sure that I understand what you don't understand, but I'll rephrase it: It will be buffered if Content-Type is not text/json. It's not a value judgement, I'm just stating the behavior. Personally, I don't want it buffered because the server that I had no control of didn't set text/json as content type, causing the response (that is not actually json, but custom line based protocol) not to come. I don't want it buffered because application logic and further action depends on something inside those 512 bytes that will never come. Think "login response" or similar.Catheterize
@Catheterize text/json isn't an official MIME type. Do your comments equally apply to application/json?Aalesund
S
2

There aren't any alternatives I'm aware of.

All the other libraries are built on top of NSURLConnection. Though you could use one of the non-iOS libraries, eg. libcurl.

ASIHTTPRequest is the only library I'm aware of that's built on top of the CFNetworking layer instead. This was (perhaps indirectly) the main reason the original developer stopped working on it - because it doesn't use NSURLConnection it has a lot of code.

It's probably not strictly correct to say that ASIHTTPRequest is no longer supported. It is true that the original developer no longer works on it, but if you look at the github commits you'll see it is still being worked on by other people. A lot of people still use it, for various reasons, myself included.

Having said all that, to go back to the problem you have: I'm not sure a 3 year old thread is necessarily a definitive reference to prove that a 1 year old release (ie. iOS 4.3) of NSURLConnection doesn't support chunked transfers. Chunked transfers are used so much on the web that it seems highly unlikely it would have a problem this large and obvious. It's possible there is something very particular to the server that you're using that is causing the issue.

Systematology answered 15/2, 2012 at 19:42 Comment(1)
I agree, and I thought I'd find a lot more if this was still a severe issue. Using ASI now means that we didn't explore it further, but just before posting this I'd begin to wonder (hence the looking for confirmation). I'll probably go back and have a look at it when I get a chance as it's preferable to use NSURLConnection if possible (smaller code base and libraries don't need to be imported etc.) Once I do I'll make sure to update this for anyone else wondering.Pulchia

© 2022 - 2024 — McMap. All rights reserved.