拦截WebView Flash插件的Web请求 [英] Intercept web requests from a WebView Flash plugin

查看:224
本文介绍了拦截WebView Flash插件的Web请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a desktop browser app which uses a WebView to host a Flash plugin. The Flash plugin makes regular requests to an external website for new data, which it then draws as fancy graphics.

我想拦截这些网络请求并获取数据(所以我可以通过Growl显示它,而不是保持桌面窗口)。但最好的,我可以告诉,Flash的请求不会被正常的WebView代理拾起。

I'd like to intercept these web requests and get at the data (so I can display it via Growl, instead of keeping a desktop window around). But best I can tell, requests made by Flash don't get picked up by the normal WebView delegates.

还有另外一个地方可以设置挂钩?我尝试通过[NSURLCache setSharedURLCache]安装自定义NSURLCache但从来没有被调用。我也试过方法swizzling其他几个类(如NSCachedURLResponse),但找不到一个方法。任何想法?非常感谢!

Is there another place I can set a hook? I tried installing a custom NSURLCache via [NSURLCache setSharedURLCache] but that never got called. I also tried method swizzling a few of the other classes (like NSCachedURLResponse) but couldn't find a way in. Any ideas? Many thanks!

推荐答案

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

Surprised no one answered this, it is actually pretty easy. Create a subclass of NSURLProtocol, and then call registerClass to start intercepting.

[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];
}

这篇关于拦截WebView Flash插件的Web请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆