我进行了很多搜索/搜索,但无法获得有关如何在UIWebview中捕获HTTP响应 header 的答案。假设我在应用启动时将用户重定向到UIWebview中的注册网关(该网关已经处于事件状态),并且当用户完成注册后,应使用在注册时分配给用户的成功唯一ID通知应用,该ID会在HTTP响应 header 中传回。

有没有使用UIWebview捕获/打印HTTP响应 header 的直接方法?

最佳答案

我喜欢Objective-C运行时。您是否有想要做的但没有API的东西? DM;人力资源。

好吧,更重要的是,这是解决方案。它将从CFNetwork发起的每个 URL响应中捕获,这是UIWebView恰好在后台使用的功能。它还将捕获AJAX请求,图像加载等。

向其添加过滤器可能与对 header 内容进行正则表达式一样容易。

@implementation NSURLResponse(webViewHack)

static IMP originalImp;

static char *rot13decode(const char *input)
{
    static char output[100];

    char *result = output;

    // rot13 decode the string
    while (*input) {
        if (isalpha(*input))
        {
            int inputCase = isupper(*input) ? 'A' : 'a';

            *result = (((*input - inputCase) + 13) % 26) + inputCase;
        }
        else {
            *result = *input;
        }

        input++;
        result++;
    }

    *result = '\0';
    return output;
}

+(void) load {
    SEL oldSel = sel_getUid(rot13decode("_vavgJvguPSHEYErfcbafr:"));

    Method old = class_getInstanceMethod(self, oldSel);
    Method new = class_getInstanceMethod(self, @selector(__initWithCFURLResponse:));

    originalImp = method_getImplementation(old);
    method_exchangeImplementations(old, new);
}

-(id) __initWithCFURLResponse:(void *) cf {
    if ((self = originalImp(self, _cmd, cf))) {
        printf("-[%s %s]: %s", class_getName([self class]), sel_getName(_cmd), [[[self URL] description] UTF8String]);

        if ([self isKindOfClass:[NSHTTPURLResponse class]])
        {
            printf(" - %s", [[[(NSHTTPURLResponse *) self allHeaderFields] description] UTF8String]);
        }

        printf("\n");
    }

    return self;
}

@end

关于iphone - UIWebView捕获响应头,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15476539/

10-12 00:16
查看更多