【问题标题】:Intercept web requests from a WebView Flash plugin拦截来自 WebView Flash 插件的 Web 请求
【发布时间】:2010-03-22 18:40:12
【问题描述】:

我有一个桌面浏览器应用程序,它使用 WebView 来托管 Flash 插件。 Flash 插件会定期向外部网站请求新数据,然后将其绘制为精美的图形。

我想拦截这些网络请求并获取数据(这样我就可以通过 Growl 显示它,而不是保留桌面窗口)。但我可以说,Flash 发出的请求不会被普通的 WebView 代表接收。

还有其他地方可以设置钩子吗?我尝试通过 [NSURLCache setSharedURLCache] 安装自定义 NSURLCache,但从未被调用。我还尝试了调配其他一些类(如 NSCachedURLResponse)的方法,但找不到方法。有什么想法吗?非常感谢!

【问题讨论】:

    标签: cocoa webkit


    【解决方案1】:

    很惊讶没有人回答这个问题,这实际上很容易。创建NSURLProtocol的子类,然后调用registerClass开始拦截。

    [NSURLProtocol registerClass:[MyCustomURLProtocol class]];
    

    以下是子类的重要部分:

    #define REQUEST_HEADER_TAG  @"x-mycustomurl-intercept"
    
    + (BOOL)canInitWithRequest:(NSURLRequest*)theRequest
    {
        // Check for the custom header on the request to break the 
        // infinite loop created by the [startLoading] below.
        if ([theRequest valueForHTTPHeaderField:REQUEST_HEADER_TAG]) {
            return NO;
        }
    
        if ([theRequest.URL.scheme caseInsensitiveCompare:@"http"] == NSOrderedSame) {
            return YES;
        }
        return NO;
    }
    
    + (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)theRequest
    {
        return theRequest;
    }
    
    - (id)initWithRequest:(NSURLRequest*)theRequest
           cachedResponse:(NSCachedURLResponse*)cachedResponse
                   client:(id<NSURLProtocolClient>)client
    {
        // Add a custom header on the request to break the 
        // infinite loop created by the [startLoading] below.
        NSMutableURLRequest* newRequest = [theRequest mutableCopy];
        [newRequest setValue:@"" forHTTPHeaderField:REQUEST_HEADER_TAG];
    
        // Now continue the process with this "tagged" request
        self = [super initWithRequest:theRequest 
                       cachedResponse:cachedResponse 
                               client:client];
        if (self) {
           // capture the data received 
           [self setRequest:newRequest];
           receivedData = [[NSMutableData data] retain];
        }
    
        [newRequest release];
        return self;
    }
    
    - (void)dealloc
    {
        [connection release];
        [request release];
        [receivedData release];
        [super dealloc];
    }
    
    
    - (void)startLoading
    {
        // Load the data off the web as usual, but set myself up as the delegate 
        // so I can intercept the response data as it comes in.
        [self setConnection:[NSURLConnection connectionWithRequest:request delegate:self]];
    }
    
    
    - (void)stopLoading 
    {
        [connection cancel];
    }
    
    #pragma mark NSURLConnection delegate implementation
    
    - (void)connection:(NSURLConnection*)conn 
                       didReceiveResponse:(NSURLResponse*)response 
    {
        [[self client] URLProtocol:self 
                didReceiveResponse:response 
                cacheStoragePolicy:[request cachePolicy]];
        [receivedData setLength:0];
    }
    
    
    - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
    {
        [[self client] URLProtocol:self didLoadData:data];
        [receivedData appendData:data];
    }
    
    
    - (void)connectionDidFinishLoading:(NSURLConnection*)conn
    {
        [[self client] URLProtocolDidFinishLoading:self];
        [self setConnection:nil];
        if (requestTag != 0) {
            if (requestDelegate && 
                [requestDelegate respondsToSelector:
                   @selector(finishedLoadingData:forURL:taggedWith:)]) {
                [requestDelegate finishedLoadingData:receivedData 
                                              forURL:[request URL] 
                                          taggedWith:requestTag];
            }
        }
    }
    
    
    - (void)connection:(NSURLConnection*)conn didFailWithError:(NSError*)error 
    {
        [[self client] URLProtocol:self didFailWithError:error];
        [self setConnection:nil];
    }
    

    【讨论】:

    猜你喜欢
    • 2021-08-07
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2012-01-06
    相关资源
    最近更新 更多