• iOS网络请求 添加全局代理 NSURLSessionConfiguration 解决方案


    1、最近做项目、文件存储服务器是用的亚马逊的、如果直接访问、下载其资源速度很慢、所以需要在网络请求的时候添加一个代理、加速网络访问

    2、代理服务器是一个HTTPS 的一个服务器

    3、思路、解决方案、利用运行时机制(添加分类NSURLSession+Change)针对  NSURLSession 初始化添加代理对所有的网络请求进行拦截处理

    #import "NSURLSession+Change.h"
    
    @implementation NSURLSession (Change)
    +(void)load{
        Method oldMethod = class_getClassMethod(self, @selector(sessionWithConfiguration:delegate:delegateQueue:));
        Method newMethod = class_getClassMethod(self, @selector(newSessionWithConfiguration:delegate:NSURLSessiondelegateQueue:));
        
        Method oldMethod1 = class_getClassMethod(self, @selector(sessionWithConfiguration:));
        Method newMethod1 = class_getClassMethod(self, @selector(newSessionWithConfiguration:));
          
         method_exchangeImplementations(oldMethod1, newMethod1);
         method_exchangeImplementations(oldMethod, newMethod);
    }
    + (NSURLSession *)newSessionWithConfiguration:(NSURLSessionConfiguration *)configuration{
        configuration=[self newConfiguration:configuration];
        NSURLSession *section=
           [self newSessionWithConfiguration:configuration];
           return section;
    }
    + (NSURLSession *)newSessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(nullable id <NSURLSessionDelegate>)delegate NSURLSessiondelegateQueue:(nullable NSOperationQueue *)queue{
        configuration=[self newConfiguration:configuration];
        NSURLSession *section=
        [self newSessionWithConfiguration:configuration delegate:delegate NSURLSessiondelegateQueue:queue];
        return section;
    }
    #pragma mark  添加代理,加速网络请求
    +(NSURLSessionConfiguration *)newConfiguration:(NSURLSessionConfiguration *)configuration{
    //通过服务器配置HTTPS代理
        NSString *proxy=@"";
        NSInteger port=;
        configuration.connectionProxyDictionary = @{
              @"HTTPEnable":@YES,
              @"HTTPProxy":proxy,
              @"HTTPPort":@(port),
              @"HTTPSEnable":@YES,
              @"HTTPSProxy":proxy,
              @"HTTPSPort":@(port),
          };
        NSString *user=@"";
        NSString *password=@"";
        NSString* proxyIDPasswd = [NSString stringWithFormat:@"%@:%@",user,password];
        NSData* proxyoriginData = [proxyIDPasswd dataUsingEncoding:NSUTF8StringEncoding];
        NSString *base64EncodedCredential = [proxyoriginData base64EncodedStringWithOptions:0];
        NSString *authString = [NSString stringWithFormat:@"Basic: %@", base64EncodedCredential];
        configuration.HTTPAdditionalHeaders = @{@"Proxy-Authorization": authString};
        return configuration;
    }
    @end
  • 相关阅读:
    hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
    bzoj 1798: [Ahoi2009]Seq 维护序列seq 线段树 区间乘法区间加法 区间求和
    codevs 1191 树轴染色 线段树区间定值,求和
    vijos 1659 河蟹王国 线段树区间加、区间查询最大值
    tyvj:1038 忠诚 线段树 区间查询
    KL散度
    NumPy 从已有的数组创建数组
    NumPy 创建数组
    NumPy 数组属性
    NumPy 数据类型
  • 原文地址:https://www.cnblogs.com/ZhangShengjie/p/12188377.html
Copyright © 2020-2023  润新知