iOS 拦截重定向302跳转的方法详解
一:前言
公司同事在做项目的时候遇到一个302地址跳转的问题,具体需求如下:
1、公司是做WIFI覆盖的,需要在下载APP后才能注册登录以后,自动连接到外网
2、苹果底层不允许我们码农进行WIFI的切换,所以公司考虑使用302重定向来实现,我通过2种方法来实现,NSURLConnetion和NSURLSession
二:NSURLConnetion方法
大概的思路:使用NSURLConnetion类的NSURLConnectionDataDelegate中的代理方法
-(nullableNSURLRequest*)connection:(NSURLConnection*)connectionwillSendRequest:(NSURLRequest*)requestredirectResponse:(nullableNSURLResponse*)response
都是码农,直接上代码吧
新建一个DEMO项目的话,记得把AppTransportSecuritySettingsAllowArbitraryLoads设置为YES,否则无法请求网络
-(void)viewDidLoad{ [superviewDidLoad]; NSURL*url=[NSURLURLWithString:@"http://www.google.com"]; NSMutableURLRequest*quest=[NSMutableURLRequestrequestWithURL:url]; quest.HTTPMethod=@"GET"; NSURLConnection*connect=[NSURLConnectionconnectionWithRequest:questdelegate:self]; [connectstart]; } -(nullableNSURLRequest*)connection:(NSURLConnection*)connectionwillSendRequest:(NSURLRequest*)requestredirectResponse:(nullableNSURLResponse*)response{ NSHTTPURLResponse*urlResponse=(NSHTTPURLResponse*)response; NSLog(@"%ld",urlResponse.statusCode); NSLog(@"%@",urlResponse.allHeaderFields); NSDictionary*dic=urlResponse.allHeaderFields; NSLog(@"%@",dic[@"Location"]); returnrequest; }
request是你要发送的请求urlResponse.statusCode则是请求的状态码,302则是重定向。urlResponse.allHeaderFields中保存着http的头信息:
{ Connection=close; "Content-Type"="text/html"; Date="Mon,30May201604:00:49GMT"; Location="http://118.244.233.137:6001/login/?gw_address=192.168.17.1&gw_port=2060&gw_id=ctzx_11&ip=192.168.17.24&mac=54:72:4f:30:c6:10&url=http%3A%2F%2Fwww.google.com%2F"; Server="HughesTechnologiesEmbeddedServer"; }
通过urlResponse.allHeaderFields[@"Location"]即可拿到302跳转的url地址.如果需要拦截掉这个url地址跳转,则可在代理方法中自定义一个request
-(void)URLSession:(NSURLSession*)sessiontask:(NSURLSessionTask*)task willPerformHTTPRedirection:(NSHTTPURLResponse*)response newRequest:(NSURLRequest*)request completionHandler:(void(^)(NSURLRequest*__nullable))completionHandler{ //block为nil,则直接拦截掉302的url地址,防止自动跳转 completionHandler(nil); }
则直接拦截掉302的url地址,防止自动跳转completionHandler(nil);}
三:NSURLSession方法
-(void)viewDidLoad{ [superviewDidLoad]; NSURL*url=[NSURLURLWithString:@"http://www.google.com"]; NSMutableURLRequest*quest=[NSMutableURLRequestrequestWithURL:url]; quest.HTTPMethod=@"GET"; NSURLSessionConfiguration*config=[NSURLSessionConfigurationdefaultSessionConfiguration]; config.requestCachePolicy=NSURLRequestReloadIgnoringLocalCacheData; NSURLSession*urlSession=[NSURLSessionsessionWithConfiguration:configdelegate:selfdelegateQueue:[NSOperationQueuecurrentQueue]]; NSURLSessionDataTask*task=[urlSessiondataTaskWithRequest:questcompletionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror){ NSHTTPURLResponse*urlResponse=(NSHTTPURLResponse*)response; NSLog(@"%ld",urlResponse.statusCode); NSLog(@"%@",urlResponse.allHeaderFields); NSDictionary*dic=urlResponse.allHeaderFields; NSLog(@"%@",dic[@"Location"]); }]; [taskresume]; } -(void)URLSession:(NSURLSession*)sessiontask:(NSURLSessionTask*)task willPerformHTTPRedirection:(NSHTTPURLResponse*)response newRequest:(NSURLRequest*)request completionHandler:(void(^)(NSURLRequest*__nullable))completionHandler{ completionHandler(nil); }
使用方法和第一种方法一样
Demo下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。