1 // 2 // ViewController.m 3 // 01-NSURLSession請求網絡 4 // 5 // Created by kangkathy on 15/11/25. 6 // Copyright © 2015年 kangkathy. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 20 } 21 22 - (IBAction)getAction:(id)sender { 23 24 /* 25 網絡請求的流程: 26 1.構造NSURL鏈接地址 27 2.構造NSURLRequest請求對象,包含請求頭和請求體信息。 28 3.構造NSURLSessionConfiguration,可選 29 4.構造NSURLSession會話對象 30 5.建立請求任務 31 6.發送網絡請求 32 33 */ 34 35 NSURL *url = [NSURL URLWithString:@"http://piao.163.com/m/cinema/list.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000"]; 36 37 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 38 39 request.timeoutInterval = 15; 40 request.HTTPMethod = @"GET"; 41 //請求頭的設置 42 // request.allHTTPHeaderFields = @{ 43 // 44 // }; 45 // request setValue:<#(nullable NSString *)#> forHTTPHeaderField:<#(nonnull NSString *)#> 46 47 NSURLSession *session = [NSURLSession sharedSession]; 48 49 50 NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 51 //網絡請求完成,獲取響應數據後會回調的block 52 //data表示獲取的數據,response表示請求對象,error表示請求錯誤 53 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 54 //獲取狀態碼 55 NSLog(@"statusCode:%li", httpResponse.statusCode); 56 57 //獲取響應頭 58 NSDictionary *responseHeader = httpResponse.allHeaderFields; 59 NSLog(@"responseHeader:%@", responseHeader); 60 61 NSError *jsonError = nil; 62 63 NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError]; 64 65 if (!jsonError) { 66 67 //顯示咱們的數據,若是要更新UI則須要回到主線程完成。 68 NSLog(@"%@", result); 69 70 } 71 72 73 74 }]; 75 76 //發送網絡請求 77 [task resume]; 78 79 80 } 81 82 - (IBAction)postAction:(id)sender { 83 84 NSURL *url = [NSURL URLWithString:@"http://piao.163.com/m/cinema/schedule.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000"]; 85 86 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 87 88 request.timeoutInterval = 15; 89 request.HTTPMethod = @"POST"; 90 91 NSString *bodyString = @"cinema_id=1533"; 92 NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding]; 93 //POST請求的請求參數加在請求體中。 94 request.HTTPBody = bodyData; 95 96 NSURLSession *session = [NSURLSession sharedSession]; 97 98 99 NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 100 101 102 NSError *jsonError = nil; 103 104 NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError]; 105 106 if (!jsonError) { 107 108 //顯示咱們的數據,若是要更新UI則須要回到主線程完成。 109 NSLog(@"%@", result); 110 111 } 112 113 114 }]; 115 116 [task resume]; 117 118 119 120 121 122 } 123 @end