本文介绍了在嵌入式UIWebView中指定HTTP referer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 在我的应用中,我允许用户在嵌入式UIWebView中打开外部页面。我是否可以设置与该请求一起发送的referer标头?当用户打开这些外部页面时,我希望我的应用获得'信誉'。In my app, I allow the user to open up an external page in an embedded UIWebView. Is it possible for me to set the referer header that's sent with that request? I'd like for my app to get the 'cred' when the user opens up these external pages.推荐答案设置 referer 使用 - setValue:forHTTPHeaderField:NSMutableURLRequest* request = ...;[request setValue:@"https://myapp.com" forHTTPHeaderField: @"Referer"];但请注意,根据HTTP RFC,您不应该这样做,因为您的应用无法使用URI:But note that according to the HTTP RFC you shouldn't, because your app is not addressable using a URI: 如果从没有它的源获得,则不得发送Referer字段自己的URI,例如来自用户键盘的输入。 The Referer field MUST NOT be sent if the Request-URI was obtained from a source that does not have its own URI, such as input from the user keyboard. ...除非您使用自定义协议绑定到你的应用程序(myapp://blah.com/blah)。... unless you are using a custom protocol binded to your app (myapp://blah.com/blah).你可以创建一个并致电的loadRequest:手动或拦截由用户进行正常请求You can create one and call loadRequest: manually or intercepting a normal request made by the user.- (BOOL) webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType) navigationType{ NSDictionary *headers = [request allHTTPHeaderFields]; BOOL hasReferer = [headers objectForKey:@"Referer"]!=nil; if (hasReferer) { // .. is this my referer? return YES; } else { // relaunch with a modified request dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_main_queue(), ^{ NSURL *url = [request URL]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [request setHTTPMethod:@"GET"]; [request setValue:@"https://whatever.com" forHTTPHeaderField: @"Referer"]; [self.webView loadRequest:request]; }); }); return NO; }} 这篇关于在嵌入式UIWebView中指定HTTP referer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-07 13:35