iOS IP代理——基于 NSURLSession

HTTP

// 1.创建url
    // 请求一个网页
    NSString *urlString = @"http://193.112.100.126/test/testip.php";
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"210.22.176.146" forKey:@"HTTPProxy"];
    [dic setObject:[NSNumber numberWithInteger:35858] forKey:@"HTTPPort"];
    [dic setObject:[NSNumber numberWithBool:YES] forKey:@"HTTPEnable"];
    config.connectionProxyDictionary = dic;
    
    // 一些特殊字符编码
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];
    
    // 2.创建请求 并:设置缓存策略为每次都从网络加载 超时时间30秒
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
    
    // 3.session
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    
    // 4.由系统直接返回一个dataTask任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data && (error == nil)) {
            // 网络访问成功
            NSLog(@"data=%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        } else {
            // 网络访问失败
            NSLog(@"error=%@",error);
        }
    }];
    
    // 5.每一个任务默认都是挂起的,需要调用 resume 方法
    [dataTask resume];

HTTPS

// 1.创建url
    // 请求一个网页
    NSString *urlString = @"https://ip.cn";
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    [dic setObject:@"221.122.91.61" forKey:@"HTTPSProxy"];
    [dic setObject:[NSNumber numberWithInteger:80] forKey:@"HTTPSPort"];
    [dic setObject:[NSNumber numberWithBool:YES] forKey:@"HTTPSEnable"];
    
    config.connectionProxyDictionary = dic;
    
    
    // 一些特殊字符编码
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];
    
    // 2.创建请求 并:设置缓存策略为每次都从网络加载 超时时间30秒
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
    
    // 3.session
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

    
    // 4.由系统直接返回一个dataTask任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data && (error == nil)) {
            // 网络访问成功
            NSLog(@"data=%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        } else {
            // 网络访问失败
            NSLog(@"error=%@",error);
        }
    }];
    
    // 5.每一个任务默认都是挂起的,需要调用 resume 方法
    [dataTask resume];

参考资料