AFURLSessionManager 请求方法链

  • 参考http://draveness.me/afnetworking1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

- (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(nullable id)parameters
success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE;

上面实际上调用此方法,progress传nil

- (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(nullable id)parameters
progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;


上面实际上是调用的下面的方法,其中uploadProgress为nil

- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure

上面实际上是先使用`AFURLRequestSerialization`的

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
error:(NSError *__autoreleasing *)error
生成了一个NSMutalbeURLRequest

再使用

- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler;

生成了需要的DataTask

AFURLRequestSerialization

  • AFPercentEscapedStringFromString 将传入的字符串escape。因为有可能在request的form里面会有非ascii
  • AFQueryStringPair 重新定义了一个类,表示一个QueryString的key和value (实际上QueryString和formdata可以一个key多个value eg.:checkbox)
  • AFQueryStringFromParameters将一个字典转换成queryString
  • AFQueryStringPairsFromKeyAndValue 这个方法传入一个key, value, 传回了一个AFQueryStringPair的array.(里面处理了value为dictionary, array等各种嵌套情况,用一个方法递归的进行了处理)

AFStreamingMultipartFormData

AFMultipartFormData下的方法有

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- (BOOL)appendPartWithFileURL:(NSURL *)fileURL
name:(NSString *)name
error:(NSError * _Nullable __autoreleasing *)error;

- (BOOL)appendPartWithFileURL:(NSURL *)fileURL
name:(NSString *)name
fileName:(NSString *)fileName
mimeType:(NSString *)mimeType
error:(NSError * _Nullable __autoreleasing *)error;

- (void)appendPartWithInputStream:(nullable NSInputStream *)inputStream
name:(NSString *)name
fileName:(NSString *)fileName
length:(int64_t)length
mimeType:(NSString *)mimeType;

- (void)appendPartWithFileData:(NSData *)data
name:(NSString *)name
fileName:(NSString *)fileName
mimeType:(NSString *)mimeType;

- (void)appendPartWithFormData:(NSData *)data
name:(NSString *)name;

- (void)appendPartWithHeaders:(nullable NSDictionary <NSString *, NSString *> *)headers
body:(NSData *)body;

- (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes
delay:(NSTimeInterval)delay;

这个类基本上就是为了构造文件上传请求时候的multipart部分,把文件切分以及setcontenttype等细节

  • AFHTTPBodyPart这个类用来处理httpbody中的内容,对于上传文件的时候,具体boundary,如何分割,以及具体packet大小等
  • https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean/28380690#28380690有关form的具体请求细节,参见这个。在html中进行form请求,也是类似这样,可以通过chrome debugger进行查看
  • https://my.oschina.net/cnlw/blog/168466

multipart form data

  • 与普通请求独有的请求头有两个一个是Content-Type;Content-Type格式为multipart/form-data:boundary=%@
  • 一个是Content-Length,这个Content-Length,是拿的bodyStream的contentLength
  • http://www.cnblogs.com/polobymulberry/p/5170093.html

AFHTTPRequestSerializer

  • 初始化ua, 如果直接使用NSURLSession实际上系统也会用自己的UserAgent, 因为AFHTTPRequestSerializer里面可以自己custom UserAgent,所以这里直接添加了UserAgent的默认值
  • 初始化accept-language, 手动的给language加上了权重,这样服务器可以根据Accept-Language做不同的语言转换(突然想起来vapor的一个pr:https://github.com/vapor/vapor/pull/698/files)
  • AFHTTPRequestSerializerObservedKeyPaths添加了kvo
  • 作者在AFURLRequestSerialization中使用KVO监听了以下key的变化,并且在之后通过AFURLRequestSerialization serialize的所有的request中,都将监听后的值第一时间更改到request中
  • `- (void)setAuthorizationHeaderFieldWithUsername:(NSString *)username
    password:(NSString *)password` 这个`Basic Authorization`因为不是很常用,所以仅仅暴露了一个接口
    
1
2
3
4
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
URLString:(NSString *)URLString

parameters:(id)parameters
error:(NSError *__autoreleasing *)error

实际上就是生成一个NSMutableURLRequest, 然后给这个request加上本类中定义好的几个header,
然后调用- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request withParameters:(id)parameters error:(NSError *__autoreleasing *)error来生成请求。其中AFJSONRequestSerializer,AFPropertyListRequestSerializer,AFHTTPRequestSerializer, 都有实现这个方法,前两个类继承于AFHTTPRequestSerializer.其中主要区别是content-type的区别,以及在body中的数据如何处理