使用 AFNetworking3.0请求时如何获取响应头文件


根据Task强制转换:

NSHTTPURLResponse *httpURLResponse = (NSHTTPURLResponse*)task.response;

NSDictionary *allHeaderFieldsDict = httpURLResponse.allHeaderFields;


/**
* 网络请求数据总方法
*
* @param strUrl 请求网络地址和端口
* @param strApi API接口
* @param parameters 请求参数
* @param strType 请求类型:GET,POST
* @param downloadProgress 请求完成数据的进度
* @param success 响应成功时调用的Block
* @param failure 响应失败时调用的Block
* @param isEncrypt 是否解密
* @param encryptKeyStr 解密密钥
*/
+ (void)SessionManagerWebData:(NSString *)strUrl andApi:(NSString *)strApi andParameters:(NSDictionary *)parameters andRequestType:(NSString *)strType andProgress:(WebDataResponseProgress)webDataDownloadProgress andSuccess:(WebDataResponseSuccess)success andFailure:(WebDataresponseFailure)failure andIsEncrypt:(BOOL)isEncrypt andEncryptKeyStr:(NSString *)encryptKeyStr {
AFHTTPSessionManager * sessionManager = [AFHTTPSessionManager manager];
sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/xml"];
sessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];

// 设置编码
[sessionManager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

sessionManager.requestSerializer.timeoutInterval = REQUEST_TIMEOUT_VALUE;

if (![strApi hasPrefix:@"/"]) {
strApi = [NSString stringWithFormat:@"/%@", strApi];
}
NSString *requestUrl = [NSString stringWithFormat:@"%@%@", strUrl, strApi];

if ([strType isEqual:REQUEST_GET]) {
[sessionManager GET:requestUrl parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
MYLOG(@"currentThread == %@", [NSThread currentThread]);
if (webDataDownloadProgress != nil) {
webDataDownloadProgress((float)downloadProgress.completedUnitCount/(float)downloadProgress.totalUnitCount);
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 解析数据
NSDictionary *dataDict = responseObject;
if (isEncrypt) {
// data 转成 字符串
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
// 解码
// NSString *string2 = [AESCrypt decrypt:string password:encryptKeyStr];
dataDict = [string mj_JSONObject];
}

int resultCode = [[dataDict objectForKey:RESULT_STATUS] intValue];
if (resultCode == RESULT_RIGHT) {
success([dataDict objectForKey:RESULT_DATA]);
} else {
[MBProgressHUD hideHUD];
[MBProgressHUD show:[dataDict objectForKey:RESULT_MESSAGE] icon:nil view:nil];
}

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure != nil) {
failure(error);
}
MYLOG(@"error: %@", error);
[MBProgressHUD hideHUD];
[MBProgressHUD showError:@"网络异常,请稍后重试!"];
}];

} else if ([strType isEqual:REQUEST_POST]) {
[sessionManager POST:requestUrl parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
if (webDataDownloadProgress != nil) {
webDataDownloadProgress((float)uploadProgress.completedUnitCount/(float)uploadProgress.totalUnitCount);
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSHTTPURLResponse *httpURLResponse = (NSHTTPURLResponse*)task.response;
NSDictionary *dictTTTTTEMP = httpURLResponse.allHeaderFields;
// 解析数据
NSDictionary *dataDict = responseObject;
if (isEncrypt) {
// data 转成 字符串
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
// 解码
// NSString *string2 = [AESCrypt decrypt:string password:AESC_PASSWORD];
dataDict = [string mj_JSONObject];
}
if (dataDict == nil) {
[MBProgressHUD hideHUD];
if (failure != nil) {
failure(nil);
}
MYLOG(@"returnNull:%@", requestUrl);
// 1. 提示1
// [MBProgressHUD show:ERROR_SERVERS_WRONG icon:nil view:nil];
return;
}

int resultCode = [[dataDict objectForKey:RESULT_STATUS] intValue];
if (resultCode == RESULT_RIGHT) {
id object = [dataDict objectForKey:RESULT_DATA];
if (object == nil || [object isKindOfClass:[NSNull class]] || [object isEqual:@""]) {
success(nil);
} else {
success([dataDict objectForKey:RESULT_DATA]);
}
} else {
[MBProgressHUD hideHUD];
if (failure != nil) {
failure(nil);
}
// 2. 提示2
[MBProgressHUD show:[dataDict objectForKey:RESULT_MESSAGE] icon:nil view:nil];
}

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure != nil) {
failure(error);
}
MYLOG(@"error: %@", error);
[MBProgressHUD hideHUD];
// 3. 提示3
// [MBProgressHUD showError:@"网络异常,请稍后重试!"];

}];
}
}


智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告