afn3.0源码解析---AFURLRequestSerialization

AFHTTPRequestSerialization:

@方法1  

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method

URLString:(NSString *)URLString

parameters:(id)parameters

error:(NSError *__autoreleasing *)error  //设置请求方法,设置请求url,内部调用@方法2设置请求头和路径参数或者httpbody

@方法2

- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request

withParameters:(id)parameters

error:(NSError *__autoreleasing *)error//设置请求头,根据paramters拼接请求路径或设置httpbody

@方法3

- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method

URLString:(NSString *)URLString

parameters:(NSDictionary *)parameters

constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block

error:(NSError *__autoreleasing *)error//内部调用@方法1设置请求方法,设置请求url,设置请求头

调用AFStreamingMultipartFormData初始化@方法1,根据params组成AFQueryStringPair对象数组,遍历取出AFQueryPair的value和key,调用AFStreamingMultipartFormData 的@方法2拼接数据,调用block,在外部调用AFMultipartFormData协议方法,通过调用AFStreamingMultipartFormData的@方法2,拼接数据。最后调用AFStreamingMultipartFormData的@方法4,设置请求头content-type和

Content-Length以及httpbodystream

AFStreamingMultipartFormData

@方法1

- (instancetype)initWithURLRequest:(NSMutableURLRequest *)urlRequest

stringEncoding:(NSStringEncoding)encoding//持有NSMutableRequest,设置字符编码,随机设置boundary,初始化

AFMultipartBodyStream对象

@方法2

- (void)appendPartWithFormData:(NSData *)data

name:(NSString *)name//设置字典headers,key为Content-Disposition,value为:[NSString stringWithFormat:@"form-data; name=\"%@\"", name],传递headers和data调用@方法3组装数据

@方法3

- (void)appendPartWithHeaders:(NSDictionary *)headers

body:(NSData *)body//设置一个AFHTTPBodyPart对象,赋值对象的header和body属性值,调用AFMultipartBodyStream@方法3将该对象添加到AFMultipartBodyStream的HTTPBodyParts数组中

@方法4

- (NSMutableURLRequest *)requestByFinalizingMultipartFormData//设置请求头content-type和Content-Length以及httpbodystream,

AFMultipartBodyStream

@方法1

- (instancetype)initWithStringEncoding:(NSStringEncoding)encoding//设置字符编码,初始化HTTPBodyParts数组

@方法2

- (NSInteger)read:(uint8_t *)buffer

maxLength:(NSUInteger)maxLength//调用AFHTTPBodyPart@方法1逐段读书数据到buffer中

@方法3

- (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart//追加HTTPBodyPart对象到HTTPBodyParts数组

AFHTTPBodyPart

@方法1

- (NSInteger)read:(uint8_t *)buffer

maxLength:(NSUInteger)length//根据当前所处阶段值AFHTTPBodyPartReadPhase,获取该阶段的内容,如

AFEncapsulationBoundaryPhase&AFFinalBoundaryPhase就是boundary的内容,将内容转为data,调用@方法2取出data的字节赋值给buffer

AFHeaderPhase就是AFHTTPBodyPart的header值,将内容转为data,调用@方法2取出data的字节赋值给buffer

AFBodyPhase就是AFHTTPBodyPart的body值,body通过inputstream引用起来,通过NSInputStream流读取

@方法2

- (NSInteger)readData:(NSData *)data

intoBuffer:(uint8_t *)buffer

maxLength:(NSUInteger)length//取出data的字节赋值给buffer

@方法3

- (NSInputStream *)inputStream {//将body转为NSInputStream

if (!_inputStream) {

if ([self.body isKindOfClass:[NSData class]]) {

_inputStream = [NSInputStream inputStreamWithData:self.body];

} else if ([self.body isKindOfClass:[NSURL class]]) {

_inputStream = [NSInputStream inputStreamWithURL:self.body];

} else if ([self.body isKindOfClass:[NSInputStream class]]) {

_inputStream = self.body;

} else {

_inputStream = [NSInputStream inputStreamWithData:[NSData data]];

}

}

return _inputStream;

}

上一篇:Comparable和Comparator接口是干什么的?列出它们的区别。


下一篇:[LeetCode] Count Univalue Subtrees 计数相同值子树的个数