ios - RestKit 请求因 HTTP 错误而挂起

标签 ios restkit afnetworking

我正在尝试让我的应用程序处理 HTTP 客户端错误。理想情况下,我希望在发生此类错误时运行静态方法,而不管导致它的请求是什么。

看来我做错了什么 -- 我试图添加一个 RKDynamicResponseDescriptor 来调用该方法,但它没有被调用。每当我的服务器响应错误时,请求永远不会完成(网络事件指示器不断旋转并且 AFNetworkingOperationDidFinishNotification 永远不会发布。)

我可以像这样创建一个新的 AFHTTPRequest 请求来重现该行为:

request = [[RKObjectManager sharedManager].HTTPClient requestWithMethod:@"GET" path:@"events" parameters:nil];
AFHTTPRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                        NSLog(@"%@", JSON);
                    }
                    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
                    }];

successfailure block 都没有被调用。如果我用

替换第一行
request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];

block 被调用。如何让 RestKit 处理 HTTP 错误响应?

编辑 1: 显然,当服务器返回带有身份验证质询的 401 时,这两种情况都会发生。我尝试在 didReceiveAuthenticationChallenge 中的 RKHTTPRequestOperation 中设置断点,但它没有被触发。我的 Fiddler 显示只发送了一个请求。

最佳答案

经过大量的摆弄,我想我找到了这个问题的原因。 AFUrlConnectionRequest 类在启用 SSL 固定时实现 connection:willSendRequestForAuthenticationChallenge:delegate 方法。如果身份验证质询与 SSL 无关,则实现简单地返回。

来自documentation of NSUrlConnectionDelegate :

This method allows the delegate to make an informed decision about connection authentication at once. If the delegate implements this method, it has no need to implement

  • connection:canAuthenticateAgainstProtectionSpace:,
  • connection:didReceiveAuthenticationChallenge:,
  • connectionShouldUseCredentialStorage:.

In fact, these other methods are not invoked.

这些方法不会被调用,因此 RestKit 永远不知道请求导致了 HTTP 错误 401。

我通过删除这个定义解决了这个问题,所以委托(delegate)方法没有实现:

#define _AFNETWORKING_PIN_SSL_CERTIFICATES_

关于ios - RestKit 请求因 HTTP 错误而挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16855600/

相关文章:

ios - AFNetworking 发送 JSON 作为 POST 请求的参数

ios - 异步加载多个图像到单个单元格

ios - 为什么 tableview 在 iOS 6+ 模拟器上工作正常,但在 iOS 5 模拟器上它崩溃了

ios - 如何在 UITableView 单元格中居中 UIImage?

ios - 我怎样才能改进这个涉及 NSDate 比较的实现?

ios - RestKit 0.20 — 核心数据 : error: Failed to call designated initializer on NSManagedObject class (again)

ios - RestKit 0.2 'NSInternalInconsistencyException',原因 : 'Unable to add mapping for keyPath scheduleEntries, one already exists...'

ios - RestKit 新手指南

ios - 在 AFNetworking 中将 NSNumber 的 NSArray 作为参数传递的正确方法

ios - 如何使布局保持不变,但根据设备增加/减小尺寸?