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沒有這個功能,您可以嘗試使用 AFNetworking