美文网首页
AFNetworking

AFNetworking

作者: 携一两本单色书来 | 来源:发表于2020-01-09 15:07 被阅读0次

    AFHTTPRequestSerializer 请求序列化

    初始化,大概干了这么些事情

    • Accept-Language :前五种语言
    • User-Agent :项目名称?:bundleID 版本号 model ios 系统号 scale
    • 监听暴漏在外部的六个属性
     self.stringEncoding = NSUTF8StringEncoding;
        self.mutableHTTPRequestHeaders = [NSMutableDictionary dictionary];
        self.requestHeaderModificationQueue = dispatch_queue_create("requestHeaderModificationQueue", DISPATCH_QUEUE_CONCURRENT);
        NSMutableArray *acceptLanguagesComponents = [NSMutableArray array];
        ///获取前五中语言
        [[NSLocale preferredLanguages] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            float q = 1.0f - (idx *0.1f);
            [acceptLanguagesComponents addObject:[NSString stringWithFormat:@"%@;q=%0.1g",obj,q]];
            *stop = q<=0.5f;
        }];
        ///设置前五种语言
        [self setValue:[acceptLanguagesComponents componentsJoinedByString:@", "] forHTTPHeaderField:@"Accept-Language"];
        NSString *userAgent = nil;
    #if TARGET_OS_IOS
        ///ebooksystem       /4.0.0 (iPhone; iOS 13.3; Scale/2.00) ios.zaxue.zaxue_ios
        ///项目名称?:bundleID 版本号   model   ios 系统号 scale
        userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
    #elif TARGET_OS_WATCH
        // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
        userAgent = [NSString stringWithFormat:@"%@/%@ (%@; watchOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[WKInterfaceDevice currentDevice] model], [[WKInterfaceDevice currentDevice] systemVersion], [[WKInterfaceDevice currentDevice] screenScale]];
    #elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
        userAgent = [NSString stringWithFormat:@"%@/%@ (Mac OS X %@)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[NSProcessInfo processInfo] operatingSystemVersionString]];
    #endif
        if (userAgent) {
            if (![userAgent canBeConvertedToEncoding:NSASCIIStringEncoding]) {
                NSMutableString *mutableUserAgent = [userAgent mutableCopy];
                if (CFStringTransform((__bridge CFMutableStringRef)(mutableUserAgent), NULL, (__bridge CFStringRef)@"Any-Latin; Latin-ASCII; [:^ASCII:] Remove", false)) {
                    userAgent = mutableUserAgent;
                }
            }
            [self setValue:userAgent forHTTPHeaderField:@"User-Agent"];
        }
        self.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET",@"HEAD",@"DELETE",nil];
        self.mutableObservedChangedKeyPaths = [NSMutableSet set];
        ///监听六个属性
        for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths()) {
            if ([self respondsToSelector:NSSelectorFromString(keyPath)]) {
                [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:AFHTTPRequestSerializerObserverContext];
            }
        }
    

    requestWithMethod

    数据放在contentType中,直接用body上传

     - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
     URLString:(NSString *)URLString
    parameters:(id)parameters error:(NSError *__autoreleasing *)error
    

    生成request的基本方法

    - (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
                                   withParameters:(id)parameters
                                            error:(NSError *__autoreleasing *)error
    

    multipart 流传输 用于大文件

    Content-Type:multipart/form-data;boundary=

    - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
                    URLString:(NSString *)URLString
                   parameters:(NSDictionary *)parameters
    constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                                                      error:(NSError *__autoreleasing *)error { NSParameterAssert(method);
        NSParameterAssert(![method isEqualToString:@"GET"] && ![method isEqualToString:@"HEAD"]);
        NSMutableURLRequest *mutableRequest = [self requestWithMethod:method URLString:URLString parameters:nil error:error];
        
        __block AFStreamingMultipartFormData *formData = [[AFStreamingMultipartFormData alloc] initWithURLRequest:mutableRequest stringEncoding:NSUTF8StringEncoding];
    //将传入字典转为AFQueryStringPair
        if (parameters) {
            for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {
                NSData *data = nil;
                if ([pair.value isKindOfClass:[NSData class]]) {
                    data = pair.value;
                } else if ([pair.value isEqual:[NSNull null]]) {
                    data = [NSData data];
                } else {
                    data = [[pair.value description]dataUsingEncoding:self.stringEncoding];
                }
    ///拼接到data中
    if (data) {
                    [formData appendPartWithFormData:data name:[pair.field description]];
                }
            }
        }
        if (block) {
            block(formData);
        }
        ///返回最终拼好流和body的 request
        return [formData requestByFinalizingMultipartFormData];
    }
    

    来进一步分析里面干了些什么

    1. 先根据requestWithMethod:method生成一个request
     NSMutableURLRequest *mutableRequest = [self requestWithMethod:method URLString:URLString parameters:nil error:error];
    
     __block AFStreamingMultipartFormData *formData = [[AFStreamingMultipartFormData alloc] initWithURLRequest:mutableRequest stringEncoding:NSUTF8StringEncoding];
    

    AFStreamingMultipartFormData

    AFStreamingMultipartFormData干了些什么呢?

    1. 核心方法是这个,设置BodyStream ,其中 self.boundary 和 bodyStream是其两个属性,返回设置bodyStream 的 request
    - (NSMutableURLRequest *)requestByFinalizingMultipartFormData
    

    appendPartWithFileURL 是谁调用呢?

    1. 遵守 AFMultipartFormData 的代理方 外部调用 appendPartWithFileData 方法传入Data数据并将数据转为AFHTTPBodyPart 模型

    然后appendHTTPBodyPart 将body 加到 AFMultipartBodyStream 的 HTTPBodyParts body数组中

    AFMultipartBodyStream

    用于存放AFHTTPBodyPart的数组
    @property (readwrite, nonatomic, strong) NSMutableArray *HTTPBodyParts;
    

    外部暴漏的方法

    初始化stream
    - (instancetype)initWithStringEncoding:(NSStringEncoding)encoding;
    ///HTTPBodyParts的第一个元素设置开始属性为YES,把最后一个元素设置结束属性为YES  AFStreamingMultipartFormData返回Request时 和打开流 时 会调用
    - (void)setInitialAndFinalBoundaries;
    /// HTTPBodyParts 添加 HTTPBodyParts
    - (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart;
    
    ///重写了NSInputStream的 读取拼接数据
    - (NSInteger)read:(uint8_t *)buffer
            maxLength:(NSUInteger)length
    ///拼接下一段body
    
    

    AFHTTPBodyPart

    外部暴漏方法

    ///转移到下一个阶段
    - (BOOL)transitionToNextPhase;
    ///模型拼接Data数据
    - (NSInteger)readData:(NSData *)data
               intoBuffer:(uint8_t *)buffer
                maxLength:(NSUInteger)length;
    - (BOOL)transitionToNextPhase
     对Multipart请求体各部分(初始边界、头部、内容数据实体、结束边界)做拼接和读取的封装
    
    AFHTTPRequestSerializer逻辑图

    相关文章

      网友评论

          本文标题:AFNetworking

          本文链接:https://www.haomeiwen.com/subject/hbrsactx.html