AFNetworking3.0请求序列化<四>

前言

这个类的作用就是把请求的URL路径 及对应的参数 封装成OC中的NSURLRequest,兼容了GET HEAD DELETE 及其他方式如POST,也开放了属性可以设置请求体,如默认会携带cookies 超时时间。。

层级关系
AFHTTPRequestSerializer
AFPropertyListRequestSerializer AFJSONRequestSerializer

一 初始化

可以看到在最初创建AFHTTPSessionManager的时候,也会创建一个默认的请求及响应的序列化类 从中可看出主要是设置了Accept-Language和userAgent两个请求体字段信息,另外也使用KVO技术监听了他6的属性的变化,

/**
    是否允许使用设备的蜂窝移动网络来创建request,默认为允许:
 */
@property (nonatomic, assign) BOOL allowsCellularAccess;

/**
 创建的request所使用的缓存策略,默认使用`NSURLRequestUseProtocolCachePolicy`,该策略表示
 如果缓存不存在,直接从服务端获取。如果缓存存在,会根据response中的Cache-Control字段判断
 下一步操作,如: Cache-Control字段为must-revalidata, 则 询问服务端该数据是否有更新,无更新话
 直接返回给用户缓存数据,若已更新,则请求服务端
 */
@property (nonatomic, assign) NSURLRequestCachePolicy cachePolicy;

/**
 如果设置HTTPShouldHandleCookies为YES,就处理存储在NSHTTPCookieStore中的cookies
 HTTPShouldHandleCookies表示是否应该给request设置cookie并随request一起发送出去
 */
@property (nonatomic, assign) BOOL HTTPShouldHandleCookies;

/**
 HTTPShouldUsePipelining表示receiver(理解为iOS客户端)的下一个信息是否必须等到上一个请求回复才能发送。
 如果为YES表示可以,NO表示必须等receiver收到先前的回复才能发送下个信息。
 默认 是NO
 */
@property (nonatomic, assign) BOOL HTTPShouldUsePipelining;

/**
 设定request的network service类型. 默认是`NSURLNetworkServiceTypeDefault`.
 这个network service是为了告诉系统网络层这个request使用的目的
 比如NSURLNetworkServiceTypeVoIP表示的就这个request是用来请求网际协议通话技术(Voice over IP)。
 系统能根据提供的信息来优化网络处理,从而优化电池寿命,网络性能等等
 */
@property (nonatomic, assign) NSURLRequestNetworkServiceType networkServiceType;

/**
   超时机制 默认60秒
 */
@property (nonatomic, assign) NSTimeInterval timeoutInterval;
Paste_Image.png

AFURLRequestSerialization协议

Paste_Image.png

AFHTTPRequestSerializer init 方法实现

- (instancetype)init
{
 if (self = [super init])
 {
  self.stringEncoding = NSUTF8StringEncoding;
  
  self.mutableHTTPRequestHeaders = [NSMutableDictionary dictionary];
  //  创建一个并行队列
  self.requestHeaderModificationQueue = dispatch_queue_create("requestHeaderModificationQueue", DISPATCH_QUEUE_CONCURRENT);
  
  // Accept-Language HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
//  传递可接受的语言,q代表对语言的喜好程度,默认是取出前5个的数据,不足5个,取实际的个数
  NSMutableArray *acceptLanguagesComponents = [NSMutableArray array];
  [[NSLocale preferredLanguages] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
   float q = 1.0f - (idx * 0.1f);
   [acceptLanguagesComponents addObject:[NSString stringWithFormat:@"%@;q=%0.1g", obj, q]];
   *stop = q <= 0.5f;
  }];
  /*
   acceptLanguagesComponents:<__NSArrayM 0x17024a500>(
   zh-Hans-CN;q=1,    简体中文
   en-CN;q=0.9,
   zh-Hant-CN;q=0.8   繁体中文
   )
   
   */
  [self setValue:[acceptLanguagesComponents componentsJoinedByString:@", "] forHTTPHeaderField:@"Accept-Language"];
  
  NSString *userAgent = nil;
#if TARGET_OS_IOS
  // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
  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
  // 例如 userAgent:     JZAXBZS/1.1.3 (iPhone; iOS 10.2; Scale/2.00)
  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;
    }
   }
   // User-Agent意义:
   //使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等
   [self setValue:userAgent forHTTPHeaderField:@"User-Agent"];
  }
  
  // HTTP Method Definitions; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
  self.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET", @"HEAD", @"DELETE", nil];
  
  self.mutableObservedChangedKeyPaths = [NSMutableSet set];
  //  对 AFHTTPRequestSerialize  的各项属性进行KVO监听
  for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths())
  {
   if ([self respondsToSelector:NSSelectorFromString(keyPath)])
   {
    [self addObserver:self forKeyPath:keyPath
        options:NSKeyValueObservingOptionNew
        context:AFHTTPRequestSerializerObserverContext];
   }
  }
 
 }
    return self;
}

二 根据参数创建请求

这是这个类最核心的方法,看下他是怎么实现的

    NSURL *url = [NSURL URLWithString:URLString];
    NSParameterAssert(url);
    NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url];
    // 设置请求方式
    mutableRequest.HTTPMethod = method;
    // 根据初始化 AFHTTPRequestSerializer  产生的属性  设置请求头
    for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths())
    {
        if ([self.mutableObservedChangedKeyPaths containsObject:keyPath])
        {
            id value = [self valueForKeyPath:keyPath];
            [mutableRequest setValue:value forKey:keyPath];
        }
    }

Content-Type 默认值:application/x-www-form-urlencoded

        if (!query)
        {
            query = @"";
        }
        if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"])
        {
            [mutableRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        }
        NSData *httpBody = [query dataUsingEncoding:self.stringEncoding];
        [mutableRequest setHTTPBody:httpBody];

你可能感兴趣的:(AFNetworking3.0请求序列化<四>)