zoukankan      html  css  js  c++  java
  • GET-POST请求数据-01-网络

      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
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    shell Builtin variables(shell内建变量)
    [置顶] 九度笔记之 1434:今年暑假不AC
    OpenRisc-45-or1200的ID模块分析
    hdu1556 Color the ball
    PB C/S轉B/S ODBC方式連接數據庫
    Django的安装配置和开发
    通过一个月时间字段分组
    如何加入该网站for Linux(绑定域名)
    LeetCode 36 Sudoku Solver
    POJ 1986 Distance Queries LCA两点距离树
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5429842.html
Copyright © 2011-2022 走看看