我正在尝试让我的应用程序处理 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);
}];
success
和failure
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/