UIWebView捕获响应头 [英] UIWebView capturing the response headers
问题描述
我搜索/搜索了很多但无法得到关于如何在 UIWebview
中捕获HTTP响应头的答案。假设我在App Launch上的UIWebview中重定向到用户注册网关(已经处于活动状态),当用户完成注册时,应该通过在HTTP响应标头中传回的注册时分配给用户的成功唯一ID来通知应用程序。 。
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; HR。
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.
为此添加过滤器可能就像对标题内容执行正则表达式一样简单。 / p>
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 = '\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
这篇关于UIWebView捕获响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!