一 AFNetworking 请求基本流程
1 生成NSMutableURLRequest 对象
AFHTTPSessionManager 实例对象种自动创建一个 AFHTTPRequestSerializer类型的requestSerializer 对像, 如下图
2 用requestSerializer 生成一个NSMutableRequest对象
如下图 生成MutableRequest的过程中,会将requestSerializer 的 请求头字典 赋值给MutableRequest 的请求头, 如下图 并且创建请求Request之后,如果从外面传来了请求头参数,会再次给 request的请求头赋值的
3 AFHTTPSessionManager 使用MutableRequest 生成一个 请求任务 NSURLSessionDataTask
AFHTTPSessionManager 继承于AFURLSession
AFHTTPSessionManager 默认持有一个NSURLSession 对象
到这里,一个任务就创建成了
二 使用方法
1 直接使用AFHTTPSessionManager 内部生成request 的方式生成task请求
如图
2 使用AFHTTPRequestSerializer手动生成request , 然后使用 AFHTTPSessionManager生成task请求
如图,这种方式需要手动开启任务
3设置请求头
1 直接用AFHTTPSessionManager 设置请求头
2 使用request 设置请求头
设置整个请求头 也可以单独设置某个键值
3 给AFHTTPSessionManager 持有的AFHTTPRequestSerializer设置请求头字典
4 获取请求相关信息
NSHTTPURLResponse *httpURLResponse = (NSHTTPURLResponse *)task.response;
NSDictionary *allHeaderFieldsDict = httpURLResponse.allHeaderFields;
NSInteger code = httpURLResponse.statusCode;
NSString *reqId = [allHeaderFieldsDict objectForKey:ResponsePaperRequestId];
三 常见请求代码
1 普通get请求
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer.timeoutInterval = 15.f;
NSURLSessionDataTask *task = [manager GET:requestUrl parameters:parameter headers:[self requestHeaderContainContentType:YES] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[self handlesuccessResponse:task.response responseData:responseObject userInfo:userInfo];
[self performSelector:@selector(jsonFinished:) withObject:userInfo];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[self handleFailtureResponse:task.response error:error userInfo:userInfo];
[self performSelector:@selector(requestFailed:) withObject:userInfo];
}];
2 普通post 请求
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer.timeoutInterval = 15;
[userInfo setObject:URL forKey:@"requestUrl"];
NSURLSessionTask *task = [manager POST:requestUrl parameters:parameter headers:[self requestHeaderContainContentType:NO] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[self handlesuccessResponse:task.response responseData:responseObject userInfo:userInfo];
[self performSelector:@selector(jsonFinished:) withObject:userInfo];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[self handleFailtureResponse:task.response error:error userInfo:userInfo];
[self performSelector:@selector(requestFailed:) withObject:userInfo];
}];
3 post body 请求
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:requestUrl parameters:nil error:nil];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
if (parameterString) {
NSData *paramData =[parameterString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:paramData];
} else {
[request setHTTPBody:[NSMutableData data]];
}
[request setAllHTTPHeaderFields:[self requestHeaderContainContentType:NO]];
[[manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (!error) {
[self handlesuccessResponse:response responseData:responseObject userInfo:userInfo];
[self performSelector:@selector(jsonFinishedWithBlock:) withObject:userInfo];
} else {
[self handleFailtureResponse:response error:error userInfo:userInfo];
[self performSelector:@selector(requestFailed:) withObject:userInfo];
}
}] resume];
4 下载请求
NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"GET" URLString:requestUrl parameters:nil error:nil];
[request setAllHTTPHeaderFields:[self requestHeaderContainContentType:YES]];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLSessionTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSURL *pathURL = [NSURL URLWithString:[@"file://" stringByAppendingString:filePath]];
return pathURL;
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (!error) {
[self performSelector:@selector(downloadFinish:) withObject:userInfo];
} else {
[self handleFailtureResponse:response error:error userInfo:userInfo];
[self performSelector:@selector(requestFailed:) withObject:userInfo];
}
}] ;
[task resume];
四 常见报错
1 “NSLocalizedDescription” : @“Request failed: unacceptable content-type: text/html”
就可以了
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:requestUrl parameters:parameter headers:[self requestHeaderContainContentType:NO] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[self handlesuccessResponse:task.response responseData:responseObject userInfo:userInfo];
[self performSelector:@selector(afxmlRequestFinished:) withObject:userInfo];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[self handleFailtureResponse:task.response error:error userInfo:userInfo];
[self performSelector:@selector(requestFailed:) withObject:userInfo];
}];
|