服务器之家

服务器之家 > 正文

iOS 拦截重定向302跳转的方法详解

时间:2021-03-16 15:40     来源/作者:Biny

一:前言

公司同事在做项目的时候遇到一个302地址跳转的问题,具体需求如下:

1、公司是做WIFI覆盖的,需要在下载APP后才能注册登录以后,自动连接到外网

2、苹果底层不允许我们码农进行WIFI的切换,所以公司考虑使用302重定向来实现,我通过2种方法来实现,NSURLConnetion和NSURLSession

二:NSURLConnetion方法

大概的思路:使用NSURLConnetion类的NSURLConnectionDataDelegate中的代理方法

 

复制代码 代码如下:

- (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response

 

都是码农,直接上代码吧

新建一个DEMO项目的话,记得把App Transport Security Settings Allow Arbitrary Loads设置为YES,否则无法请求网络

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- (void)viewDidLoad {
  [super viewDidLoad];
 NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
  NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url];
  quest.HTTPMethod = @"GET";
  NSURLConnection *connect = [NSURLConnection connectionWithRequest:quest delegate:self];
  [connect start];
}
 
- (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response{
 
  NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
 
  NSLog(@"%ld",urlResponse.statusCode);
  NSLog(@"%@",urlResponse.allHeaderFields);
  NSDictionary *dic = urlResponse.allHeaderFields;
  NSLog(@"%@",dic[@"Location"]);
  return request;
}

request 是你要发送的请求 urlResponse.statusCode 则是请求的状态码,302则是重定向 。urlResponse.allHeaderFields中保存着http的头信息:

?
1
2
3
4
5
6
7
{
  Connection = close;
  "Content-Type" = "text/html";
  Date = "Mon, 30 May 2016 04:00:49 GMT";
  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 = "Hughes Technologies Embedded Server";
}

通过urlResponse.allHeaderFields[@"Location"]即可拿到302跳转的url地址.如果需要拦截掉这个url地址跳转,则可在代理方法中自定义一个request

?
1
2
3
4
5
6
7
8
9
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
    newRequest:(NSURLRequest *)request
 completionHandler:(void (^)(NSURLRequest * __nullable))completionHandler{
 
  //block为nil,则直接拦截掉302的url地址,防止自动跳转
  completionHandler(nil);
 
}

则直接拦截掉302的url地址,防止自动跳转 completionHandler(nil);}

三:NSURLSession方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
- (void)viewDidLoad {
  [super viewDidLoad];
  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
  NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url];
  quest.HTTPMethod = @"GET";
  NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue currentQueue]];
 
  NSURLSessionDataTask *task = [urlSession dataTaskWithRequest:quest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
 
    NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
 
    NSLog(@"%ld",urlResponse.statusCode);
    NSLog(@"%@",urlResponse.allHeaderFields);
 
    NSDictionary *dic = urlResponse.allHeaderFields;
    NSLog(@"%@",dic[@"Location"]);
 
  }];
 
  [task resume];
 
}
 
 
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
    newRequest:(NSURLRequest *)request
 completionHandler:(void (^)(NSURLRequest * __nullable))completionHandler{
 
  completionHandler(nil);
 
}

使用方法和第一种方法一样

Demo下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。​

原文链接:http://www.jianshu.com/p/ee695f842ff9?utm_source=tuicool&utm_medium=referral

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部