什么是 NSURLConnection 的分块传输编码的替代方案 [英] What are alternatives to NSURLConnection for chunked transfer encoding

查看:30
本文介绍了什么是 NSURLConnection 的分块传输编码的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了与此相关的其他问题,但唯一的答案是使用 ASIHTTPRequest",因为它不再被开发,我想问问人们正在使用哪些替代方案,同时致力于我们的 SDK 在从服务器接收数据时,我在 NSURLConnection 中遇到了很多奇怪的行为.

我们将其追溯到这样一个事实:NSURLConnection 不能很好地处理分块编码中的响应.或者至少我们在这里阅读了这个问题 NSURLConnection 和chunked"传输编码

我们采访的一些开发者说它在 iOS 5 中变得更好,我们需要确保我们的 SDK 至少向后兼容 iOS 4.3.

我想确认这实际上是 NSURLConnection 中的一个问题,以及人们如何处理它.

到目前为止,我发现的所有替代方案都基于 NSURLConnection,并且我假设这样也会有相同的缺陷.ASIHTTPRequest 实际上确实有效,因为它的基础比 NSURLConnection 低一点,但是在它不再受支持的情况下正在寻找替代方案.

查看的其他库列表是:Restkit,ShareKit,LRResty,AFNetworking,TTURLRequest

我知道这里有类似的问题 RESTKit 是一个很好的替代品吗?ASIHTTPRequest? 和这里 ASIHTTPRequest 替代方案 但是这两种解决方案都基于 NSURLConnection.

我注意到我在帖子开头指出了错误的问题,所以更新了.它指向 2008 年的一个帖子,我看过类似的帖子,但没有一个是最近的.

解决方案

NSURLConnection 支持分块传输.我用它们.

  1. 定义一些道具:

    <块引用>

    NSMutableData * responseData;NSURLConnection * 连接;

  2. 建立连接

    <块引用>

    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. 为建立的连接注册回调方法

    <块引用>

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {//您可能在这里收到了 HTTP 200,或者没有...[响应数据集长度:0];}

  4. 为接收到块"注册您的回调方法

    <块引用>

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {NSString* aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];NSLog(@"这是我的第一块 %@", aStr);}

  5. 注册您的连接完成"回调:

    <块引用>

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {【连接释放】;}

  6. 最后,注册连接失败"回调:

<块引用>

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {NSLog(@"出了点问题...");}

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.

解决方案

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...");
}

这篇关于什么是 NSURLConnection 的分块传输编码的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆