AFHTTPSessionManager 是AFURLSessionManager的子類,因為這個類可以更加方便通過http來進(jìn)行請求,get或者post的方法。
ios6 之前os x10.8 都是使用AFHTTPSessionOperationManager 具有相同的作用;
為了個可以get、post方法更加方便,我們重寫了dataTAskWithRequest:completionHandler: 這個方法。AFURLRequestSerialization 發(fā)送請求,系列化,因為http客戶端會有相應(yīng)的默認(rèn)的頭和編碼參數(shù),AFURLResponseSerialization 相應(yīng)返回。
NSURL+urlWithString 相關(guān)的方法更加的方便
AFHTTPSessionManager 類
@interface AFHTTPSessionManager : AFURLSessionManager <NSSecureCoding, NSCopying>
1)@property (readonly, nonatomic, strong, nullable) NSURL//網(wǎng)絡(luò)請求路徑鏈接 *baeURL;
requestWithMethod:URLString:parameters:(常用語這個方法)
這個方法中使用基本的url,用于發(fā)送網(wǎng)絡(luò)請求
requestWithMethod:URLString:parameters:
& multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:
2) @property (nonatomic, strong) AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer;
默認(rèn)的頭部使用系列化參數(shù)通過這個參數(shù)指定這個屬性,系列化了字符串參數(shù)的請求 ,
使用的方式是get,head,delete,url-form-encodes http信息體
3)@property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;
響應(yīng)的系列化的信息題 (這個對象對象和上面的的對象是對應(yīng)的)
dataTaskWithRequest:success:failure 這個方法中,
4)+ (instancetype)manager;
創(chuàng)建一個 AFHTTPSessionManager 對象, (也就是http回話管理對象)
5)- (instancetype)initWithBaseURL:(nullable NSURL *)url;
初始化一個AFHTTPSessionManager 對象,這個對象帶有url,http客戶端的
6)- (instancetype)initWithBaseURL:(nullable NSURL *)url
sessionConfiguration:(nullable NSURLSessionConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
返回一個信息的http客戶端,通過一個基本的url,回話配置NSURLSessionConfiguration來創(chuàng)建一個信息的會話管理。
【上面的3個方法都是創(chuàng)建一個AFHTTPSessionManager回話管理對象】
http的網(wǎng)路請求
<1>http的get請求
創(chuàng)建NSURLSessionDataTask(會話的數(shù)據(jù)任務(wù)),
- (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;
網(wǎng)絡(luò)請求中的get的方法,
成功的block:一個task 、 和一個responseObject的系列化返回對象
失敗的block:一個task、 和一個error的返回的碼 (e rror describing the network or parsing error that occurred.也就是這個錯誤就是用來描述網(wǎng)絡(luò)好解析的錯誤)
8)
創(chuàng)建NSURLSessionDataTask(會話的數(shù)據(jù)任務(wù)),(多了一個NSProgress的類對象,系統(tǒng)自帶的一個方法)
- (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;
只是這里的區(qū)別:
(這就是用于下載更新的,被調(diào)用在一個會話隊列中,但不是主隊列)
<2>http的 head的網(wǎng)絡(luò)請求
@see -dataTaskWithRequest:completionHandler:
*/
- (
nullable NSURLSessionDataTask *)HEAD:(NSString
*)URLString
parameters:(
nullable id
)parameters
success:(
nullable void (^)(NSURLSessionDataTask *task))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
9)http:的post的請求方法
@see -dataTaskWithRequest:completionHandler:
*/
-
(
nullable NSURLSessionDataTask *)POST:(NSString
*)URLString
parameters:(
nullable id
)parameterssuccess:(
nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE;
@see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
*/這個請求是帶有下載的請求的方式:
- (
nullable NSURLSessionDataTask *)POST:(NSString
*)URLString
parameters:(
nullable id
)parameters progress:(
nullable void (^)(NSProgress
*uploadProgress)) uploadProgress success:(
nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
@see -dataTaskWithRequest:completionHandler:
*/待有form的方式來進(jìn)行發(fā)送網(wǎng)絡(luò)請求:
- (
nullable NSURLSessionDataTask *)POST:(NSString
*)URLString
parameters:(
nullable id
)parameters
constructingBodyWithBlock:(
nullable void (^)(id <AFMultipartFormData
formData))block
success:(
nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE;
@see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
*/這種方式是增加了uploadProgress 、 AFMultipartFormData的數(shù)據(jù)結(jié)構(gòu),以及我們的下載的方式進(jìn)行
- (
nullable NSURLSessionDataTask *)POST:(NSString
*)URLString
parameters:(
nullable id
)parameters
constructingBodyWithBlock:(
nullable void (^)(id <AFMultipartFormData
formData))block
progress:(
nullable void (^)(NSProgress
*uploadProgress)) uploadProgress
success:(
nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
<3>下面是http的put的方式來發(fā)送網(wǎng)路請求
@see -dataTaskWithRequest:completionHandler:
*/
- (
nullable NSURLSessionDataTask *)PUT:(NSString
*)URLString
parameters:(
nullable id
)parameters success:(
nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
<4>
@see -dataTaskWithRequest:completionHandler:
*/這個是patch的發(fā)送的網(wǎng)路請求
- (
nullable NSURLSessionDataTask *)PATCH:(NSString
*)URLString
parameters:(
nullable id
)parameters
success:(
nullable void (^)(NSURLSessionDataTask *task, id _Nullable
responseObject))success
failure:(
nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
<5>
下面是delete的方式進(jìn)行發(fā)送網(wǎng)路的請求
@see -dataTaskWithRequest:completionHandler:
*/
- (nullable NSURLSessionDataTask )DELETE:(NSString)URLString parameters:(nullable id)parameters success:(
nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success failure:(
nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError
*error))failure;
@end
AFHTTPSessionManager 這個類中基本是可以進(jìn)行相關(guān)的內(nèi)容來進(jìn)行實現(xiàn)的。
——> 這個類中基本上就是對這個相關(guān)的回話內(nèi)容來發(fā)送網(wǎng)絡(luò)請求的