1伴逸、AFHTTPRequestOperationManager的封裝了與web應(yīng)用程序通過HTTP通信的常見模式,包括創(chuàng)建請求,響應(yīng)序列化,網(wǎng)絡(luò)可達性監(jiān)控缠沈、運營管理和安全,以及請求。
2、針對ios7或Mac OS X 10.9或更高版本的開發(fā)人員洲愤,推薦使用其子類“AFHTTPSessionManager”進行web服務(wù)颓芭,AFHTTPSessionManager提供了一個類方法,這個類方法方法返回一個共享的單例對象柬赐,在該對象上可以通過應(yīng)用程序共享身份驗證和其他配置亡问。
3、想要改變AFHTTPRequestOperationManager及其子類的所有請求操作的結(jié)構(gòu)肛宋,那么就需要重載方法HTTPRequestOperationWithRequest:success:failure
州藕。
4、由HTTP客戶端所創(chuàng)建的請求會包括請求頭和根據(jù)requestSerializer屬性加密后的參數(shù)酝陈,這個請求符合AFURLRequestSerialization規(guī)范床玻。
5、收到的返回結(jié)果會符合AFURLResponseSerialization規(guī)范后添,這個結(jié)果會被服務(wù)器端自動驗證并且被responseSerializers序列化笨枯。
6薪丁、
對于HTTP方便的方法來說遇西,請求序列化從相對路徑(the path relative)和基本路徑(-baseURL)兩個屬性去構(gòu)造URLs,構(gòu)造URLs用的就是方法NSURL +URLWithString:relativeToURL:
,前提是這兩個屬性都被提供的情況。如果baseURL是空严嗜,構(gòu)造URLs就需要使用NSURL +URLWithString:
方法去獲得一個有效的URL粱檀。
以下就是怎樣使用baseURL和relative paths合成URL的方法:
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
[NSURL URLWithString:@"foo" relativeToURL:baseURL]; // http://example.com/v1/foo
[NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz
[NSURL URLWithString:@"/foo" relativeToURL:baseURL]; // http://example.com/foo
[NSURL URLWithString:@"foo/" relativeToURL:baseURL]; // http://example.com/v1/foo
[NSURL URLWithString:@"/foo/" relativeToURL:baseURL]; // http://example.com/foo/
[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/
7、通過“reachabilityManager”屬性可獲得網(wǎng)絡(luò)可達性狀態(tài)和變更監(jiān)控漫玄。應(yīng)用程序可以選擇監(jiān)視網(wǎng)絡(luò)可達性條件茄蚯,以防止或中止任何出站請求。
8睦优、“AFHTTPRequestOperationManager”符合“NSSecureCoding”和“NSCopying”協(xié)議,允許Operation歸檔到磁盤,分別存檔和復(fù)制在內(nèi)存中渗常。不過,有幾個小問題需要謹記:
- HTTP客戶端的存檔和副本將使用空操作隊列初始化汗盘。
- NSSecureCoding不能序列化/反序列化block屬性皱碘,因此HTTP客戶端的存檔將不包括可能設(shè)置的任何可達性block屬性。
9隐孽、
/**
The dispatch queue for the `completionBlock` of request operations. If `NULL` (default), the main queue is used.
*/
//默認回調(diào)主線程
@property (nonatomic, strong) dispatch_queue_t completionQueue;
/**
The dispatch group for the `completionBlock` of request operations. If `NULL` (default), a private dispatch group is used.
*/
//默認開啟一個私有g(shù)roup
@property (nonatomic, strong) dispatch_group_t completionGroup;
首先癌椿,看一個普通的GET請求:
- (AFHTTPRequestOperation *)GET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
//操作隊列加入請求操作
[self.operationQueue addOperation:operation];
return operation;
}
//AFHTTPRequestSerializerObservedKeyPaths???
static NSArray * AFHTTPRequestSerializerObservedKeyPaths() {
static NSArray *_AFHTTPRequestSerializerObservedKeyPaths = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_AFHTTPRequestSerializerObservedKeyPaths = @[NSStringFromSelector(@selector(allowsCellularAccess)), NSStringFromSelector(@selector(cachePolicy)), NSStringFromSelector(@selector(HTTPShouldHandleCookies)), NSStringFromSelector(@selector(HTTPShouldUsePipelining)), NSStringFromSelector(@selector(networkServiceType)), NSStringFromSelector(@selector(timeoutInterval))];
});
return _AFHTTPRequestSerializerObservedKeyPaths;
}
原來它是一個方法的字符串數(shù)組。再這里菱阵,需要看一下類AFURLRequestSerialization踢俄。
“AFHTTPRequestSerializer”符合“AFURLRequestSerialization”&“AFURLResponseSerialization”協(xié)議,兩者提供了URL的查詢字符串/表單編碼的參數(shù)序列化和默認請求頭,以及響應(yīng)狀態(tài)代碼和內(nèi)容類型驗證的一個具體的基本實現(xiàn)方法晴及。這個類包含了一些屬性都办,這些屬性就是上面提到的字符串數(shù)組:
/**
The string encoding used to serialize parameters. `NSUTF8StringEncoding` by default.
*/
@property (nonatomic, assign) NSStringEncoding stringEncoding;
/**
Whether created requests can use the device’s cellular radio (if present). `YES` by default.
@see NSMutableURLRequest -setAllowsCellularAccess:
*/
@property (nonatomic, assign) BOOL allowsCellularAccess;
/**
The cache policy of created requests. `NSURLRequestUseProtocolCachePolicy` by default.
@see NSMutableURLRequest -setCachePolicy:
*/
@property (nonatomic, assign) NSURLRequestCachePolicy cachePolicy;
/**
Whether created requests should use the default cookie handling. `YES` by default.
@see NSMutableURLRequest -setHTTPShouldHandleCookies:
*/
@property (nonatomic, assign) BOOL HTTPShouldHandleCookies;
/**
Whether created requests can continue transmitting data before receiving a response from an earlier transmission. `NO` by default
@see NSMutableURLRequest -setHTTPShouldUsePipelining:
*/
@property (nonatomic, assign) BOOL HTTPShouldUsePipelining;
然后,在AFHTTPRequestSerializer類里,封裝了Request請求如下:
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
error:(NSError *__autoreleasing *)error
{
NSParameterAssert(method);
NSParameterAssert(URLString);
NSURL *url = [NSURL URLWithString:URLString];
NSParameterAssert(url);
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url];
mutableRequest.HTTPMethod = method;
//目的是觀察看是否有RequestSerialization屬性值的改變琳钉。
//為什么要寫成字符串數(shù)組的格式世舰?
for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths()) {
if ([self.mutableObservedChangedKeyPaths containsObject:keyPath]) {
[mutableRequest setValue:[self valueForKeyPath:keyPath] forKey:keyPath];
}
}
mutableRequest = [[self requestBySerializingRequest:mutableRequest withParameters:parameters error:error] mutableCopy];
return mutableRequest;
}
到這里,發(fā)現(xiàn)要形成一個AFHTTPRequestOperation對象槽卫,就是封裝NSMutableURLRequest的同時跟压,再加上添加一個請求參數(shù)的序列化類(AFHTTPRequestSerializer)以及對該類的屬性值做一些設(shè)置以滿足AFRequestOperation更好的更多的功能和用戶體驗。
在這里歼培,每一個mutableRequest都會設(shè)置一個緩存策略震蒋,且有一個默認值, NSURLRequestUseProtocolCachePolicy。
可是躲庄,我有一個疑問:AFHTTPRequestOperationManager為什么要使用NSCopying協(xié)議(對象拷貝協(xié)議)呢查剖?
關(guān)于網(wǎng)絡(luò)請求的知識,我收集了一些佳作:
http://blog.csdn.net/lcg910978041/article/details/51484817