NSURLSession能不能规定获取数据的类型,比如说我希望获取的是xml,但是实际获取的却是json,这样就会报错
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[configuration setHTTPAdditionalHeaders:@{@"Accept": @"application/xml"}];
[configuration setHTTPAdditionalHeaders:@{@"Content-Type": @"application/xml"}];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSURLSessionDataTask *task = [session dataTaskWithURL:url
completionHandler:^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
if (error) {
NSLog(@"%@", [error localizedDescription]);
} else {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (200 != statusCode) {
NSLog(@"HTTP status code = %ld", statusCode);
success = NO;
}
NSLog(@"%@", data);
}
}];
NSURLSession tidak mempunyai fungsi ini, anda boleh cuba menggunakan AFNetworking