【问题标题】:NSURLConnection status code for Asynchronous request iOS 6异步请求 iOS 6 的 NSURLConnection 状态码
【发布时间】:2013-04-17 04:14:34
【问题描述】:

目前使用 NSURLConnection 中的 sendAsynchronous 进行 post 和 get 请求,但无法获取响应中的状态码。大多数帖子都建议使用 NSURLConnection 及其委托方法,我理解它们也是异步的。

我不明白委托如何将信息发送回调用方法(最终需要数据的方法)。 sendAsynchronous 方法有一个我现在正在使用的回调。

我是新手,谢谢你的帮助。

【问题讨论】:

    标签: cocoa-touch ios6 http-status-code-401 nsurlconnectiondelegate sendasynchronousrequest


    【解决方案1】:

    当您使用sendAsynchronousRequest:queue:completionHandle 方法时,我将尝试在该上下文中回答:

    [NSURLConnection sendAsynchronousRequest:request 
                                       queue:queue 
                           completionHandler:
        ^(NSURLResponse *response, NSData *data, NSError *error) {
    
        // This will get the NSURLResponse into NSHTTPURLResponse format
        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
    
        // This will Fetch the status code from NSHTTPURLResponse object
        int responseStatusCode = [httpResponse statusCode];
    
        //Just to make sure, it works or not
        NSLog(@"Status Code :: %d", responseStatusCode);
    }];
    

    【讨论】:

      【解决方案2】:

      我有一个包装类,它可以让这变得非常简单并保持你的代码干净: https://github.com/ricardocontrerasrobles/EasyNetwork

      【讨论】:

        猜你喜欢
        • 2011-08-24
        • 1970-01-01
        • 2013-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-13
        相关资源
        最近更新 更多