网络请求 __ NSURLSession

时间:2023-12-26 09:17:49

首先配置into.plist文件

1. 添加 App Transport Security Settings , Type栏自动变为Dictionary

2. 点击左边箭头,使之向下,点击右边加号,添加 Allow Arbitrary Loads (Xcode 7.0之后会默认填充Allow Arbitrary Loads )

3. 将value 改为YSE

-- -- 代码实现 -- --

#import "ViewController.h"

//NSURLSessionDataDelegate NSURLSession 获取网络数据的代理协议
@interface ViewController ()<NSURLSessionDataDelegate>
//保存结果的data
@property (nonatomic, strong) NSMutableData *resultData; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)post:(id)sender { NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *dataString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData *postData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData]; NSURLSessionTask *dataTask = [session dataTaskWithRequest:request]; [dataTask resume]; // NSURLSession *session = [NSURLSession sharedSession];
//
// NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
// NSURL *url = [NSURL URLWithString:urlString];
//
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//
// NSString *dataString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
// NSData *postData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
// [request setHTTPBody:postData];
// [request setHTTPMethod:@"POST"];
//
// NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// if (error == nil) {
// NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
// NSLog(@"dic == %@", dic);
// }
// }];
// [task resume];
}
- (IBAction)get:(id)sender {
// //block 回调的方式
// //使用系统提供的全局的NSURLSession对象, 是一个单例
// NSURLSession *session = [NSURLSession sharedSession];
// NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
// NSURL *url = [NSURL URLWithString:urlString];
// //NSURLSession 是基于任务的, 所以所有的东西都要放到任务里面, NSURLSessionTask就是NSURLSession 的任务执行对象
// NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// if (error == nil) {
// NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
// NSLog(@"dic == %@", dic);
// }
// }];
// //NSURLSession的所有任务默认是挂起的, 所以一定要调用resume方法, 让任务开始
// [task resume]; //NSURLSession 代理的异步操作 //NSURLSession 代理人的属性是只读的
// 参数一 : 会话模式
// 参数二 : 代理人
// 参数三 : 代理方法在哪个线程中进行
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"]; //NSURLSessionTask的子类
NSURLSessionTask *dataTask = [session dataTaskWithURL:url]; [dataTask resume]; } - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
//NSURLSession的代理协议里面 必须设置允许继续请求, 才会继续的响应服务器, 获取到数据
completionHandler(NSURLSessionResponseAllow);
self.resultData = [NSMutableData data];
} //接收到数据
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
[self.resultData appendData:data];
} //结束响应
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (error == nil) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.resultData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic == %@", dic);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end