Sample code for NSURLSessionConfiguration
// Instantiate a session configuration object.
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.timeoutIntervalForRequest = 300;
configuration.HTTPAdditionalHeaders = @{@"Accept" : @"application/xml", @"Content-Type" : @"application/xml; charset=UTF-8", @"User-Agent" : userAgent};
// Instantiate a session object.
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
// Create a data task object to perform the data downloading.
NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:"http://myexample.com"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error != nil) {
// If any error occurs then just display its description on the console.
NSLog(@"Server ERROR: \n "
"CODE: %ld, \n "
"LOCALIZED DESCRIPTION: %@ \n "
"DESCRIPTION: %@ \n "
"FAILURE REASON: %@ \n "
"RECOVERY OPTION: %@ \n "
"RECOVERY SUGGESTION: %@ \n",
(long)[error code],
[error localizedDescription],
error.description,
error.localizedFailureReason,
error.localizedRecoveryOptions,
error.localizedRecoverySuggestion);
}
else {
// If no error occurs, check the HTTP status code.
NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];
if (HTTPStatusCode == 200) {
//Initialize XML parsing with the response data
/*NSString *str = [[NSString alloc]initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"The URLSession response is: %@ \n", str);*/
}
}
}];
// Resume the task.
[task resume];