AFNetWorking初探之AFHTTPRequestOperation(二)

上篇关于AFHTTPRequestOperation的文章中我们说道

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:requst];这个方法会调用父类的方法进行一些初始化操作下面我们看看他都干了些什么


 AFHTTPRequestOperation本类调用两个方法

1.父类方法 

2.self.responseSerializer = [AFHTTPResponseSerializer serializer]。

AFHTTPRequestOperation父类AFURLConnectionOperation 中调用

1.super 

2.self.lock = [[NSRecursiveLock alloc] init];

  self.lock.name = kAFNetworkingLockName;

  self.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes];

  self.request = urlRequest;

  self.shouldUseCredentialStorage = YES;  

  self.securityPolicy = [AFSecurityPolicy defaultPolicy]; 

我们按照程序的执行顺序来看  首先或走父类AFURLConnectionOperation的super方法,即到NSOperation当中去执行 这是apple官方的不在细说。然后执行self.lock=.. self.lock.name = ..我们上次说道这是一个初始化锁的操作为保证请求的原子性,self.runLoopModes=.. 我们发现这个属性类型是一个NSSet(不允许重复的)从名字上看是一个loop猜测应该跟请求队列有关,点进去看发现如下解释 跟我们猜测的差不多。  

/**

 The run loop modes in which the operation will run on the network thread. By default, this is a single-member set containing `NSRunLoopCommonModes`.

 */

@property (nonatomic,strong) NSSet *runLoopModes;



接着往下看self.request= ... 简单的赋值操作。把我们定义的nsrequest给自己。接着是self.shouldusercreaden...= yes; 这个属性名翻译过来是用户证书应该储藏,跟证书有关的操作,给的描述如下 翻译一下就是问我们 是否让这个url connedtion 翻阅证书存储处来验证链接。 还说道这个值是NSURLConnectionDelegate中的connectionShouldUseCredentialStorage方法返回的。  

/**

 Whether the URL connection should consult the credential storage for authenticating the connection. `YES` by default.


 This is the value that is returned in the `NSURLConnectionDelegate` method `-connectionShouldUseCredentialStorage:`.

 */

@property (nonatomic,assign) BOOL shouldUseCredentialStorage;


再接着往下是self.securityPolicy = [AFSecurityPolicydefaultPolicy]
 

+ (instancetype)defaultPolicy {
    AFSecurityPolicy *securityPolicy = [[self alloc] init];
    securityPolicy.SSLPinningMode = AFSSLPinningModeNone;

    return securityPolicy;
}
干了什么呢,生成一个默认的 afsecuritypolicy对象,这个对象用来添加跟证书有关的东西,我们知道很多网络请求都是以https开头的这是为了保证我们请求的安全性,这个类给了我们一下说明,可以看出 添加ssl证书等内容。

/**

 `AFSecurityPolicy` evaluates server trust against pinned X.509 certificates and public keys over secure connections.


 Adding pinned SSL certificates to your app helps prevent man-in-the-middle attacks and other vulnerabilities. Applications dealing with sensitive customer data or financial information are strongly encouraged to route all communication over an HTTPS connection with SSL pinning configured and enabled.

 */


总算说完了AFURLConnectionOperation 我们终于可以回到AFHTTPRequestOperation了 他的里面初始化了一个东西 AFHTTPResponseSerializer 

+ (instancetype)serializer {
    return [[self alloc] init];
}

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.stringEncoding = NSUTF8StringEncoding;  //一看就知道跟编码有关

    self.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)];//状态码相关设定 解释如下
    self.acceptableContentTypes = nil;

    return self;
}

可接受的http状态码,没有包含在这个之内的将导致返回错误。


/**

 The acceptable HTTP status codes for responses. When non-`nil`, responses with status codes not contained by the set will result in an error during validation.


 See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

 */

@property (nonatomic,copy, nullable)NSIndexSet *acceptableStatusCodes;


看到这里我们明白在我们上篇关于AFHTTPRequestOperation中说道的setcompel..的block中 op的状态码决定返回错误与否的说法其实是有问题的,正确的判断错误的流程应该是 服务器返回状态码->让AFHTTPRequestOperation当中的NSHTTPURLResponse *response 的这个里面的statusCode等于服务器端的状态码->然后AFHTTPRequestOperation这个类根据response 这里面的状态码与acceptableStatusCodes里面的码对比 如果存在就block到success里面 如果错去就block到 fail里面。


 op.response.statusCode





你可能感兴趣的:(AFNetWorking初探之AFHTTPRequestOperation(二))