3

我需要拦截客户端将在应用程序中进行的网络调用,同时调用将成功完成。我发现一个解决方案是实现 NSURLProtocol 抽象类并注册到应用程序。

这似乎解决了我的问题,但后来我碰壁了,请求超时。我使用 AFNetworking 最新的 1.x 版本,因为我需要支持 iOS 5.0。

所以现在我需要找到如何延长初始请求的超时时间或另一种更合适的解决方案来解决我的问题。

这是我所做的。NSURLProtocol 实现:

@implementation SLKURLProtocol

+ (NSURLRequest*) canonicalRequestForRequest:(NSURLRequest *)request
    {
    return request;
    }

+ (BOOL) canInitWithRequest:(NSURLRequest *)request
    {
    return [[[request URL] scheme] isEqualToString:@"http"];
    }

- (void) startLoading
    {
    dispatch_async(kBgQueue,
        ^{
        NSLog(@"Intercept Request startLoading");
        NSURLRequest* request = self.request;
        NSLog(@"URL: %@", request.URL);
        NSLog(@"HTTP Method: %@", request.HTTPMethod);
        NSLog(@"Data: %@", [NSString stringWithUTF8String:[request.HTTPBody bytes]]);
        });
    }

- (void) stopLoading
    {
    dispatch_async(kBgQueue,
        ^{
        NSLog(@"Intercept Request stopLoading");
        });
    }

@end

网络视图控制器:

@implementation SLKViewController

- (void) viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [NSURLProtocol registerClass:[SLKURLProtocol class]];
    [self initRequest];
    }

- (void) initRequest
    {
    NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://requestb.in/19kcum21"]
            cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];
    [urlRequest setHTTPMethod:@"POST"];

    NSMutableData* postData = [NSMutableData data];
    [postData appendData:[@"REQUEST AFNETWORKING" dataUsingEncoding:NSUTF8StringEncoding]];
    [urlRequest setHTTPBody:postData];
    AFHTTPRequestOperation* operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];

    [operation setCompletionBlockWithSuccess:
        ^(AFHTTPRequestOperation *operation, id responseObject)
        {
        NSString* serverResponse = [operation responseString];
        NSLog(@"AFNetworking Response: %@", serverResponse);
        } failure:
            ^(AFHTTPRequestOperation *operation, NSError *error)
        {
        NSLog(@"AFNetworking Error: %@", error.description);
        }];

    [operation start];
    }

@end

这是日志输出:

2014-01-15 17:07:19.518 InterceptionHttp[510:1303] 拦截请求 startLoading 2014-01-15 17:07:19.520 InterceptionHttp[510:1303] URL: http://requestb.in/19kcum21 2014-01- 15 17:07:19.521 InterceptionHttp[510:1303] HTTP 方法:POST 2014-01-15 17:07:19.522 InterceptionHttp[510:1303] 数据:请求 AFNETWORKING 2014-01-15 17:07:29.522 InterceptionHttp[510: 400b] 拦截请求 stopLoading 2014-01-15 17:07:29.522 InterceptionHttp[510:70b] AFNetworking 错误:错误域 = NSURLErrorDomain 代码 = -1001“请求超时。” UserInfo=0x8a5cb10 {NSErrorFailingURLStringKey= http://requestb.in/19kcum21 , NSErrorFailingURLKey= http://requestb.in/19kcum21, NSLocalizedDescription=请求超时。, NSUnderlyingError=0x8a5c220 "请求超时。"}

编辑:

在 jqyao 回答并阅读文章后,我了解了一些关于子类化 NSURLProtocol 的主题。但不幸的是我仍然无法拦截HTTP,这意味着我希望原始请求能够正常继续。

同时,如果我添加调用 URLProtocol 委托选择器的 3 行,原始请求将继续,但会返回数据而不是服务器的响应。

这是我在startLoading方法中添加的内容。

[[self client] URLProtocol:self didReceiveResponse:[[NSURLResponse alloc] init] cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[[self client] URLProtocol:self didLoadData:data];
[[self client] URLProtocolDidFinishLoading:self];

这是我的输出日志。

2014-02-04 17:27:22.253 InterceptionHttp[419:3a03] 拦截请求 startLoading 2014-02-04 17:27:22.254 InterceptionHttp[419:3a03] URL: http ://requestb.in/tsvc14ts 2014-02- 04 17:27:22.255 InterceptionHttp[419:3a03] HTTP 方法:POST 2014-02-04 17:27:22.255 InterceptionHttp[419:3a03] 数据:{'message':'JSON_MESSAGE'} 2014-02-04 17: 27:22.258 InterceptionHttp[419:70b] AFNetworking 响应:{'message':'JSON_MESSAGE'} 2014-02-04 17:27:22.260 InterceptionHttp[419:1303] 拦截请求 stopLoading

4

2 回答 2

4

我建议你看看这篇文章http://robnapier.net/offline-uiwebview-nsurlprotocol和实现https://github.com/rnapier/RNCachingURLProtocol。您多久看到一次超时问题?

于 2014-01-31T17:28:34.097 回答
3

好的,我从这个非常酷的网络和数据调试组件https://github.com/square/PonyDebugger中借用了一些代码,并且做得非常好和顺利。

有关类是https://github.com/square/PonyDebugger/blob/master/ObjC/PonyDebugger/PDNetworkDomainController.m

希望对其他看起来相似的人有所帮助。

于 2014-02-26T17:41:47.017 回答