簡單網路請求/GET請求/POST請求,網路get請求post
#pragma mark - 簡單網路請求- (IBAction)simpleRequest:(id)sender { //1.構造URL NSURL *url = [NSURL URLWithString:@"http://news-at.zhihu.com/api/3/news/latest"]; //2.構造Request NSURLRequest *request = [NSURLRequest requestWithURL:url]; //3.構造Session NSURLSession *session = [NSURLSession sharedSession]; //4.構造要執行的任務task /** * task * * @param data 返回的資料 * @param response 回應標頭 * @param error 錯誤資訊 * */ NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"response: %@", response); if (error == nil) { /* NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"data: %@", dataStr); */ //json --> data 用JSON對象建立data //NSJSONSerialization *jsonData = [NSJSONSerialization dataWithJSONObject:<#(id)#> options:<#(NSJSONWritingOptions)#> error:<#(NSError *__autoreleasing *)#>] /* options: 1.讀取reading NSJSONReadingMutableContainers 產生可變的對象,不設定這個option,預設是建立不可變對象 NSJSONReadingMutableLeaves 產生可變的字串MutableString(iOS7+有bug) NSJSONReadingAllowFragments 允許json資料最外層不是字典或者數組 2.寫入writing NSJSONWritingPrettyPrinted 產生json資料是格式化的,有換行,可讀性高 */ //data --> json 用data建立json NSJSONSerialization *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"json: %@", jsonData); //JSONKit 開源架構 } else { //處理相應的錯誤 } }]; //5.發送請求 [task resume]; }#pragma mark - GET請求- (IBAction)getRequest:(id)sender { //GET請求, 也可以給伺服器發送資訊, 也有參數(微博使用者名稱,使用者id) //1.構造URL, 參數直接拼接在url串連後 NSURL *url = [NSURL URLWithString:@"http://news-at.zhihu.com/api/3/news/4602734"]; //2.構造Request //把get請求的要求標頭儲存在request裡 //NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 參數 // (1)url // (2)緩衝策略 // (3)逾時的時間, 經過120秒之後就放棄這次請求 //NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:120]; //NSURLRequest 不可變,不能動態添加要求標頭資訊 //可變的對象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //(1)佈建要求方式 [request setHTTPMethod:@"GET"]; //(2)逾時時間 [request setTimeoutInterval:120]; //(3)緩衝策略 [request setCachePolicy:NSURLRequestReturnCacheDataElseLoad]; //(4)佈建要求頭其他內容 //[request setValue:<#(NSString *)#> forHTTPHeaderField:<#(NSString *)#>]; //[request addValue:<#(NSString *)#> forHTTPHeaderField:<#(NSString *)#>]; //[request setAllHTTPHeaderFields:<#(NSDictionary *)#>]; [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]; //告訴服務,返回的資料需要壓縮 //3.構造Session NSURLSession *session = [NSURLSession sharedSession]; //4.Task NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (error == nil) { NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"data: %@", dataStr); } }]; //5. [task resume];}#pragma mark - POST請求- (IBAction)postRequest:(id)sender { //POST請求 //1.構造URL NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/2/statuses/update.json"]; //2.構造Request NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //(1)設定為POST請求 [request setHTTPMethod:@"POST"]; //(2)逾時 [request setTimeoutInterval:60]; //(3)佈建要求頭 //[request setAllHTTPHeaderFields:nil]; //(4)佈建要求體 //發微博 //請求體裡需要包含至少兩個參數 //access_token 指定使用者的令牌(這個以後會講) //status 微博本文 NSString *bodyStr = @"access_token=XXXXX&status=我們想要發送的內容"; NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:bodyData]; //3.構造Session NSURLSession *session = [NSURLSession sharedSession]; //4.task NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"response : %@", response); }]; //5. [task resume]; }