SoFunction
Updated on 2025-04-13

Detailed explanation of iOS's method of intercepting redirection 302 jump

1: Preface

When working on a project, my colleagues encountered a 302 address jump problem. The specific needs are as follows:

1. The company is doing WIFI coverage. You need to download the APP before you can register and log in and automatically connect to the external network.

2. Apple's underlying layer does not allow us coders to switch WIFI, so the company considers using 302 redirection to implement it. I implemented it through 2 methods, NSURLConnetion and NSURLSession

Two: NSURLConnetion method

A general idea: Use the proxy method in NSURLConnectionDataDelegate of the NSURLConnetion class

Copy the codeThe code is as follows:

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

They are all coders, just upload the code

If you create a new DEMO project, remember to set App Transport Security Settings Allow Arbitrary Loads to YES, otherwise the network cannot be requested.

- (void)viewDidLoad {
  [super viewDidLoad];
 NSURL *url = [NSURL URLWithString:@""];
  NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url];
   = @"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",);
  NSLog(@"%@",);
  NSDictionary *dic = ;
  NSLog(@"%@",dic[@"Location"]);
  return request;
}

request is the request you want to send, which is the status code of the request, and 302 is redirection. The header information of http is saved:

{
  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%%2F";
  Server = "Hughes Technologies Embedded Server";
}

You can get the url address with 302 jump through [@"Location"]. If you need to intercept the jump of this url address, you can customize a request in the proxy method.

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
    newRequest:(NSURLRequest *)request
 completionHandler:(void (^)(NSURLRequest * __nullable))completionHandler{

  //Block is nil, and the URL address of 302 is directly intercepted to prevent automatic jumps  completionHandler(nil);

}

Then directly intercept the url address of 302 to prevent automatic jump of completionHandler(nil);}

Three: NSURLSession method

- (void)viewDidLoad {
  [super viewDidLoad];
  NSURL *url = [NSURL URLWithString:@""];
  NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url];
   = @"GET";
  NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
   = 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",);
    NSLog(@"%@",);

    NSDictionary *dic = ;
    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);

}

How to use it is the same as the first method

Demo Download

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more. ​