UIWebView 捕获响应标头

UIWebView capturing the response headers(UIWebView 捕获响应标头)

本文介绍了UIWebView 捕获响应标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索/谷歌了很多,但无法得到关于如何在 UIWebview 中捕获 HTTP 响应标头的答案.假设我在应用程序启动时将用户重定向到 UIWebview 中的注册网关(已经处于活动状态),当用户完成注册时,应通知应用程序,并在注册时向用户分配成功的唯一 ID,该 ID 在 HTTP 响应标头中传回.

I searched/googled a lot but could not get the answer on how to capture HTTP response headers in UIWebview. Say I redirect to user to registration gateway(which is already active) in UIWebview on App Launch and when user finishes the registration, the app should be notified with the successful unique id assigned to the user on registration which is passed back in HTTP Response Headers.

是否有任何直接的方法可以使用 UIWebview 捕获/打印 HTTP 响应标头?

Is there any direct way to capture/print the HTTP Response headers using UIWebview?

推荐答案

我喜欢 Objective-c 运行时.有什么你想做但没有 API 的吗?DM;人力资源部.

I love the objective-c runtime. Is there something that you want to do but don't have an API for it? DM;HR.

好的,更严肃地说,这是解决此问题的方法.它将捕获从 CFNetwork 发起的每个 URL 响应,这正是 UIWebView 在幕后使用的.它还将捕获 AJAX 请求、图像加载等.

Alright, on a more serious note, here's the solution to this. It will capture every URL Response that initiated from CFNetwork, which is what UIWebView happens to use behind the scenes. It will also capture AJAX requests, image loads, and more.

为此添加过滤器可能就像对标头的内容执行正则表达式一样简单.

Adding a filter to this should probably be as easy as doing a regex on the contents of the headers.

@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 = '';
    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("
");
    }

    return self;
}

@end

这篇关于UIWebView 捕获响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIWebView 捕获响应标头

基础教程推荐